Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

Combo Sort Algorithm in C

Sorting algorithms can be mixed and matched to yield the desired properties. We want fast average performance, good worst case performance, and no large extra storage requirement. We can achieve the goal by starting with the Quicksort (fastest on average). We modify Quicksort by sorting small partitions by using Insertion Sort (best with small partition). If we detect two partitions are badly balanced, we sort the larger partition by Heapsort (good worst case performance). Of course we cannot undo the bad partitions, but we can stop the possible degenerate case from continuing to generate bad partitions.

#include <stdlib.h>
#include <stdio.h>
 
 
#define uint32 unsigned int

Shellsort Algorithm in C

Sort every Nth element in an array using insertion sort. Repeat using smaller N values, until N = 1. On average, Shellsort is fourth place in speed. Shellsort may sort some distributions slowly.

#include <stdlib.h>
#include <stdio.h>
 
 
#define uint32 unsigned int

Heapsort Algorithm in C

Form a tree with parent of the tree being larger than its children. Remove the parent from the tree successively. On average, Heapsort is third place in speed. Heapsort does not need extra buffer, and performance is not sensitive to initial distributions.

#include <stdlib.h>
#include <stdio.h>
 
 
#define uint32 unsigned int

Mergesort Algorithm in C

Start from two sorted runs of length 1, merge into a single run of twice the length. Repeat until a single sorted run is left. Mergesort needs N/2 extra buffer. Performance is second place on average, with quite good speed on nearly sorted array. Mergesort is stable in that two elements that are equally ranked in the array will not have their relative positions flipped.

#include <stdlib.h>
#include <stdio.h>


#define uint32 unsigned int

Quicksort Algorithm in C

Partition array into two segments. The first segment all elements are less than or equal to the pivot value. The second segment all elements are greater or equal to the pivot value. Sort the two segments recursively. Quicksort is fastest on average, but sometimes unbalanced partitions can lead to very slow sorting.
 
#include <stdlib.h>
#include <stdio.h>
 
#define INSERTION_SORT_BOUND 16 /* boundary point to use insertion sort */
 
#define uint32 unsigned int

Insertion Sort Algorithm in C

Scan successive elements for out of order item, then insert the item in the proper place. Sort small array fast, big array very slowly.

#include <stdlib.h>
#include <stdio.h>
 
 
#define uint32 unsigned int

Selection Sort Algorithm in C

Find the largest element in the array, and put it in the proper place. Repeat until array is sorted. This is also slow.

#include <stdlib.h>
#include <stdio.h>
 
 
#define uint32 unsigned int

Bubble Sort Algorithm in C

Exchange two adjacent elements if they are out of order. Repeat until array is sorted. This is a slow algorithm.

#include <stdlib.h>
#include <stdio.h>


#define uint32 unsigned int

Linked list and Double linked list in C#

Below are two examples of implementing a linked and double linked list in .NET. The framework already has a LinkedList implementation from version 2.0 - it is infact a double linked list and supports a whole lot of features the example below doesn't. I wrote the code below out of curiosity more than anything (some programmers like doing this kind of thing, some don't!). Most degree students or earlier will have learnt about linked lists in their courses, some may have not.

Implements the LinkedList data structure

Here is the sample Code:

using System;

class Node {
  internal Object data;
  internal Node next;

  public Node(Object o, Node n){
    data = o;
    next = n;
  }
}
public class LinkedList {

Linked List on C Sharp

What we going to make is a linked list. Yeah I know there is a class which does the same as a linked list called ArrayList, but we (as a diehard C# programmer) want absolute control and knowledge of what we use.

First what is a linked list?

A linked list is a dynamically sized array. An array has a size and you have to deal with it you cant just resize an array. Unlike an array a linked list has no absolute size. It can hold as many variables as you want it to.

C on Singly linked lists

Linked lists are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary. Specifically, the programmer writes a struct or class definition that contains variables holding information about something, and then has a pointer to a struct of its type. Each of these individual struct or classes in the list is commonly known as a node.

Linked List C++ Simple Code

/*PRORGRAM LINKED-LIST
_ _ _ _ _ _ _ _ _ _ _ _ */

#include
#include
#include
#include
class Node
{
public:
int x;
Node *ka;
};
void main()

Complete Single Linked List Program in C

Description : Single linked list inplementation using different functions
1.INSERT A NUMBER AT THE BEGINNING
2.INSERT A NUMBER AT LAST
3.INSERT A NUMBER AT A PARTICULAR LOCATION IN LIST
4.PRINT THE ELEMENTS IN THE LIST 5.PRINT THE TOTAL NUMBER OF ELEMENTS IN THE LIST
6.DELETE A NODE IN THE LINKED LIST
7.REVERSE A LINKED LIST
8.GET OUT OF LINKED LIST (BYEE BYEE):

A linked list on C

Although linked lists sounds kind of scary, don't worry they are really easy to use once you've got a little practice under your belt! When I first learned this odd way of storing data, I really thought that I wouldn't be using them again. I certainly learned differently! Linked lists form the foundation of many data storing schemes in my game!

They are really nice when you don't know how many of a data type you will need, and don't want to waste space. They are like having a dynamically allocated string that fluctuates in size as the program runs. Before I really confuse you lets get into a better explanation!

Building a binary tree in C

The following program shows how to build a binary tree in a C program. It uses dynamic memory allocation, pointers and recursion. A binary tree is a very useful data-structure, since it allows efficient insertion, searching and deletion in a sorted list. As such a tree is essentially a recursively defined structure, recursive programming is the natural and efficient way to handle it.

Building a linked list in C

The following program shows how a simple, linear linked list can be constructed in C, using dynamic memory allocation and pointers.
#include
#include

struct list_el {
int val;
struct list_el * next;
};

Linked List in C#

The following code demonstrates how to write and use a link list in C#.

using System;

namespace ThisExample
{
class LinkList
{
public string Val;
public object NextItem;
[STAThread]
static void Main(string[] args)

Linked List with C

  • Linked lists are the most basic self-referential structures. Linked lists allow you to have a chain of structs with related data.
  • So how would you go about declaring a linked list? It would involve a struct and a pointer:
    struct llnode {
     data;
    struct llnode *next;
    };
    
    The signifies data of any type. This is typically a pointer to something, usually another struct. The next line is the next pointer to another llnode struct. Another more convenient way using typedef:
    typedef struct list_node {
     data;
    struct list_node *next;
    } llnode;
    
    llnode *head = NULL;
    
    Note that even the typedef is specified, the next pointer within the struct must still have the struct tag!