Thursday, May 20, 2010

Sorting

Good site : http://www.cs.auckland.ac.nz/software/AlgAnim/sorting.html

BiG o Notation : http://www.youtube.com/watch?v=6Ol2JbwoJp0&feature=channel

video : Big O, Big Omega, and Big Theta Notation

1. Bubble Sort

Video : http://www.youtube.com/watch?v=vxENKlcs2Tw&feature=related

for i = n down to 1
for j = 1 to i-1
if A[j] < v="Fr0SmtN0IJM&feature="related" and="" an="" unsorted="" x="">= i */
for(i=1;i v ) {
a[j] = a[j-1]; j = j-1;
if ( j <= 0 ) break; } /* Stopped when a[j-1] <= v, so put v at position j */ a[j] = v; } } 3. selection sort Video : http://www.youtube.com/watch?v=TW3_7cD9L1A&feature=channel Selection sort is the most conceptually simple of all the sorting algorithms. It works by selecting the smallest (or largest, if you want to sort from big to small) element of the array and placing it at the head of the array. Then the process is repeated for the remainder of the array; the next largest element is selected and put into the next slot, and so on down the line. The idea of selection sort is rather simple: we repeatedly find the next largest (or smallest) element in the array and move it to its final position in the sorted array. Assume that we wish to sort the array in increasing order, i.e. the smallest element at the beginning of the array and the largest element at the end. We begin by selecting the largest element and moving it to the highest index position. We can do this by swapping the element at the highest index and the largest element. We then reduce the effective size of the array by one element and repeat the process on the smaller (sub)array. The process stops when the effective size of the array becomes 1 (an array of 1 element is already sorted). void selection_sort(int x[], int lim) { int i, eff_size, maxpos, tmp; for (eff_size = lim; eff_size > 1; eff_size--) {
for (i = 0; i < maxpos =" x[i]"> x[maxpos] ? i : maxpos;
tmp = x[maxpos];
x[maxpos] = x[eff_size - 1];
x[eff_size - 1] = tmp;
}
}

4 Quick Sort :
Video : good one : YouTube - QuickSort
Video : http://www.youtube.com/watch?v=y_G9BkAm6B8&NR=1&feature=fvwp
video : http://www.youtube.com/watch?v=vxENKlcs2Tw&feature=related



5. Merg Sort :

Video : http://www.youtube.com/watch?v=7i8V9wLJPEg&feature=channel

6. Heap sort :

Video : http://www.youtube.com/watch?v=WYII2Oau_VY&feature=related

7. Dijkstra's Algorithm

Video : http://www.youtube.com/watch?v=8Ls1RqHCOPw&feature=related

No comments:

Post a Comment