Sunday, December 11, 2011

Sorting Algorithms

Write  functions for the following sorting algorithms

Comparison Sorts with O(n^2) worst case
http://www.geeksforgeeks.org/lower-bound-on-comparison-based-sorting-algorithms/
http://www.geeksforgeeks.org/stability-in-sorting-algorithms/

1.Bubble/ Brick /Comb sort
This is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. After each pass 1st element reaches its correct position in the array.
http://quiz.geeksforgeeks.org/bubble-sort/
http://www.geeksforgeeks.org/odd-even-sort-brick-sort/
http://www.geeksforgeeks.org/comb-sort/
http://www.geeksforgeeks.org/cocktail-sort/

2.Selection & pancake Sorting
http://quiz.geeksforgeeks.org/selection-sort/
http://www.geeksforgeeks.org/which-sorting-algorithm-makes-minimum-number-of-writes/
http://www.geeksforgeeks.org/pancake-sorting/
http://www.geeksforgeeks.org/a-pancake-sorting-question/

3.Insertion & Shell
http://quiz.geeksforgeeks.org/insertion-sort/
http://www.geeksforgeeks.org/time-complexity-insertion-sort-inversions/
http://quiz.geeksforgeeks.org/binary-insertion-sort/
http://quiz.geeksforgeeks.org/shellsort/

4.Quick
http://quiz.geeksforgeeks.org/quick-sort/
http://www.geeksforgeeks.org/iterative-quick-sort/
http://www.geeksforgeeks.org/3-way-quicksort/

http://www.geeksforgeeks.org/when-does-the-worst-case-of-quicksort-occur/
http://www.geeksforgeeks.org/comparator-function-of-qsort-in-c/
http://www.geeksforgeeks.org/can-quicksort-implemented-onlogn-worst-case-time-complexity/
http://www.geeksforgeeks.org/quicksort-tail-call-optimization-reducing-worst-case-space-log-n/

Why Quick Sort is better:
https://www.quora.com/Why-is-quicksort-considered-to-be-better-than-merge-sort
http://www.geeksforgeeks.org/why-quick-sort-preferred-for-arrays-and-merge-sort-for-linked-lists/
http://cs.stackexchange.com/questions/3/why-is-quicksort-better-than-other-sorting-algorithms-in-practice
https://learn.hackerearth.com/forum/369/how-quicksort-is-better-than-heapsort/

Qsort on Linked List
http://www.geeksforgeeks.org/quicksort-on-singly-linked-list/
http://www.geeksforgeeks.org/quicksort-for-linked-list/

5.Heap Sort
http://quiz.geeksforgeeks.org/heap-sort/

6.Merge Sort
http://quiz.geeksforgeeks.org/merge-sort/
http://www.geeksforgeeks.org/iterative-merge-sort/

7.Binary Tree Sort
http://www.geeksforgeeks.org/tree-sort/
8.Patience Sorting
https://en.wikipedia.org/wiki/Patience_sorting
9.Tournament Sort
http://www.geeksforgeeks.org/tournament-tree-and-binary-heap/

Non-Comparison Sorts
1.Counting Sort
http://www.geeksforgeeks.org/counting-sort/
2.Bucket Sort
http://www.geeksforgeeks.org/radix-sort/
3.Radix Sort
http://www.geeksforgeeks.org/bucket-sort-2/
4. Pigeonhole Sort
http://www.geeksforgeeks.org/pigeonhole-sort/

Sorting Info :
Exchange Sorts:  Bubble,Quick,Odd-even
Selection:             Selection,Heap,Tournament
Insertion:             Insertion,Shell,Tree,Patience
Merge:                 Merge
Distribution:        Bucket,Counting,Radix

Useful Links:
http://en.wikipedia.org/wiki/Sorting_algorithm
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/sortingIntro.htm

10 comments:

  1. Quick Sort -------------
    http://www.seeingwithc.org/topic2html.html

    ReplyDelete
  2. Counting Sort----------

    #define M 3
    int main()
    {
    int arr[]={0,1,2,0,0,0,1,1,2,2,2};
    int count[M];
    int i,j,len;
    len = sizeof(arr)/sizeof(arr[0]);
    printf("len:%d\n",len);
    for(i=0;i < M;i++)
    count[i]=0;
    for(i=0;i < len;i++)
    ++count[arr[i]];
    printf("I m here\n");
    for(i=0,j=0;j < M;j++)
    {
    while(count[j]--)
    arr[i++] = j;
    }
    printf("I m here\n");
    for(i=0;i < len;i++)
    printf("arr:%d\n",arr[i]);
    return 0;
    }

    ReplyDelete
  3. Quick Sort--------

    #include < iostream >
    #include < stdlib.h >
    #include < conio.h >

    int Partition(int a[] , int low, int high)
    {
    int left,right,temp;
    int pivot_item;
    pivot_item=a[low];
    left=low;
    right=high;

    while(left < right)
    {while(a[left] < =pivot_item)
    left++;
    while(a[right] > pivot_item)
    right--;
    if(left < right)
    {temp=a[left];
    a[left]=a[right];
    a[right]=temp;}

    }

    a[low]=a[right];
    a[right]=pivot_item;
    return right;
    }


    int QuickSort(int a[], int low, int high)
    { int pivot;
    if(high > low)
    { pivot=Partition(a,low,high);
    QuickSort(a,low,pivot-1);
    QuickSort(a,pivot+1,high);
    }
    }



    int main()
    {
    int n,low,high,i;

    int a[]={54,23,98,143,2,46,34,76};
    n= sizeof(a)/sizeof(a[0]);

    printf("Initial Order of elements\n");
    for(i=0;i < n;i++)
    printf("%d\n",a[i]);

    QuickSort(a,0,n-1);

    printf("Final Array After Sorting:\n");
    for(i=0;i < n;i++)
    printf("%d\n",a[i]);

    getch();
    return 0;
    }

    ReplyDelete
  4. int Partition(int a[] , int low, int high)
    {
    int left,right,temp;
    int pivot_item;
    pivot_item=a[low];
    left=low;
    right=high;

    while(left < right)
    {while(a[left] < =pivot_item)
    left++;
    while(a[right] > pivot_item)
    right--;
    if(left < right)
    {temp=a[left];
    a[left]=a[right];
    a[right]=temp;}

    }

    a[low]=a[right];
    a[right]=pivot_item;
    return right;
    }


    int QuickSort(int a[], int low, int high)
    { int pivot;
    if(high > low)
    { pivot=Partition(a,low,high);
    QuickSort(a,low,pivot-1);
    QuickSort(a,pivot+1,high);
    }
    }

    ReplyDelete
  5. http://blog4preps.blogspot.in/search/label/Sorting%20Techniques

    ReplyDelete
  6. Hi,

    Thanks for sharing this article. Very helpful.
    http://www.flowerbrackets.com/bubble-sort-program-in-java/

    Cheers

    ReplyDelete
  7. Hi,

    Awesome!! You got the best questions and answers for java interview. You're doing a great job.
    Thanks for sharing.

    Cheers,
    http://www.flowerbrackets.com/insertion-sort-in-java/

    ReplyDelete
  8. All the sorting algorithms and links are too good. Very useful.

    Thanks for sharing.
    http://www.flowerbrackets.com/best-way-to-implement-selection-sort-in-java/

    ReplyDelete
  9. You have the best collection of links for sorting algorithms. Thanks for sharing.

    Cheers,
    http://www.flowerbrackets.com/how-to-implement-quick-sort-in-java-program/

    ReplyDelete
  10. This post has best links with elaborate explanation on sorting algorithms. Thanks for sharing.

    Cheers,
    http://www.flowerbrackets.com/java-merge-sort/

    ReplyDelete