Skip to main content

Posts

Showing posts from May, 2011

Function to sort an array using insertion sort

Insertion sort is slightly better sorting method among O(n2) algorithms. It is effecient if the elements are almost sorted and if the size is small. Insertion sort basically works by splitting your elements into two parts. Sorted half and unsorted half. To begin with, Sorted half is empty and unsorted half has all elements. Then one element is removed from unsorted half and added to the sorted half. When adding , this element is inserted at the correct location. This process is continued till we have all the elements in sorted half and unsorted half is empty. Now let us try to understand it with a diagram. First 17 is removed from unsorted array and added to sorted array. Since there is one element there, it is sorted. Next 4 which is front most element now in unsored array is removed and added to sorted array. But 4 is smaller than 17, so 17 is pushed to right and its place is taken by 4.    Next 32 is removed from the front of unsorted array and added to sorted array. But here in sor

Function to sort an array using bubble sort

Quick and dirty way of sorting an array is bubble sort. It is very easy to write and follow. But please keep in mind that it is not at all effecient. #include<iostream> using std::cin; using std::cout; void readArray(int arr[],int sz); void printArray(int arr[],int sz); void sortArray(int arr[],int sz); void swap(int &a,int &b); int main() {    int sz;    cout<<"Size of the array=";    cin>>sz;    int arr[sz];    readArray(arr,sz);     sortArray(arr,sz);   cout<<"Sorted array is ";   printArray(arr,sz); } void readArray(int arr[],int sz) {  for(int i=0;i<sz;i++)    {       cout<<"arr["<<i<<"]=";       cin>>arr[i];   } } void printArray(int arr[],int sz) {  for(int i=0;i<sz;i++)    {       cout<<"arr["<<i<<"]=";       cout<<arr[i]<<"\n";   } } void sortArray(int arr[],int sz) {     for(int i=0;i<sz-1;i++)    {         for(i

Find middle node of a linked list

How do you find the middle node of singly linked list of unspecified length, by traversing the list only once? Solution: Take two pointers p1 and p2.                Let p1 and p2 point to head p1=p2 = head; Advance p1 by one node at a time and p2  by 2 nodes at a time                            p1 = p1->next;                           p2 = p2 ->next->next;               When p2 reaches NULL, p1 will be in the middle node. Here is the code for the same NODEPTR find_mid_node (NODEPTR head) { NODEPTR t1 ,t2; t1 = t2 = head; while (t2 != NULL && t2 -> next != NULL ) { t1 = t1 -> next; t2 = t2 -> next -> next; } return t1; //this is the midnode } How do you reverse a singly linked list? I have written a recursive solution for reversing a list in this post . Let us discuss non-recursive solution now.  Take two pointers p1 and p2 pointing to first and second nodes         p1 = head; p2 = p1->next; p1-&

Program to delete a node from linked list

How do you remove a node from a linked list? If you have to delete a node, first you need to search the node. Then you should find its previous node. Then you should link the previous node to the next node. If node containing 8 has to be deleted, then n1 should be pointing to n2. Looks quite simple. Isn't it? But there are at least two special cases you have to consider. Of course, when the node is not found. If the node is first node of the list viz head. If the node to be deleted is head node, then if you delete, the list would be lost. You should avoid that and make the second node as the head node. So it becomes mandatory that you return the changed head node from the function.   Now let us have a look at the code. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node * next; }; typedef struct node * NODEPTR; NODEPTR create_node ( int value) { NODEPTR temp = (NODEPTR) malloc( size