10. To introduce notion of algorithm as means of specifying how to solve a problem.
11.
12. Types Of Sorting Bubble Sort Speed: O(n^2), extremely slow but stable sorting algorithm Space: The size of initial array Coding Complexity: Simple Quick Sort Speed: O(nlogn), one of the best sorting algorithm for unsorted data Space: The size of initial array Coding Complexity: Complex, Divide and Conquer Merge Sort Speed: O(nlogn) , one of the best stable sorting algorithm Space: The size of initial array Coding Complexity: Complex, Merging Heap Sort Speed: O(nlogn) , one of the best unstable sorting algorithm Space: The size of linked list used Coding Complexity: Complex, Uses Tree Data structure
13. Using Predefined Header File Sometimes it is needed to reduce the hassle of recoding same thing over and over again. Sometimes it is needed to sort a more complex stuffs, such as record, structure etc. where these record / structure might be sorted based on key present in the record/structure. Quick Sort header for C++ Language. qsort(<arrayname>,<size>,sizeof(<elementsize>),compare_function);
14. Compare function intcompare_function(const void *a,const void *b) { // The comparison Code goes here. } Return type of the compare_function 1. negative, if a comes before b 2. 0, if a == b 3. positive, if a comes after b Using Predefined Header File
15. Using Predefined Header File Comparing a list of integers 1. simply cast a and b to integers 2. x < y, x-y is negative x == y, x-y is zero x > y, x-y is positive
16. Using Predefined Header File Comparing a list of strings Comparing floating point numbers
17. Using Predefined Header File Multi field sorting Before Sorting After Sorting
20. Inversion Counting (Merge Sort) 4 7 6 2 1 5 8 3 Divide into front and back halves 4 7 6 2 1 5 8 3 4 3 2 4 6 7 1 3 5 8 Count inversions within each half, and sort each half Then, count inversions between front and back half
21. Inversion Counting (Merge Sort) 2 4 6 7 front: 1 3 5 8 back: Merge. Whenever a back item is pushed up, count how many it goes past
22. Inversion Counting (Merge Sort) 2 4 6 7 front: 1 3 5 8 back: skips over 2, 4, 6 and 7 Inversions: 4 Merge. Whenever a back item is pushed up, count how many it goes past
23. Inversion Counting (Merge Sort) 2 4 6 7 front: 1 3 5 8 back: from front, nothing to count Inversions: 4 Merge. Whenever a back item is pushed up, count how many it goes past
24. Inversion Counting (Merge Sort) 4 6 7 front: 1 2 3 5 8 back: from front, nothing to count Inversions: 4 Merge. Whenever a back item is pushed up, count how many it goes past
25. 4 6 7 3 5 8 Inversion Counting (Merge Sort) front: 1 2 back: 3 skips over 4, 6, and 7 Inversions: 4 Inversions: 7 Merge. Whenever a back item is pushed up, count how many it goes past
26. 4 6 7 3 5 8 Inversion Counting (Merge Sort) front: 1 2 back: 4 is in front: no change Inversions: 4 Inversions: 7 Merge. Whenever a back item is pushed up, count how many it goes past
27. 4 6 7 3 5 8 Inversion Counting (Merge Sort) front: 1 2 back: 5 moves past 6 and 7 Inversions: 4 Inversions: 7 Merge. Whenever a back item is pushed up, count how many it goes past
28. 4 6 7 3 5 8 Inversion Counting (Merge Sort) front: 1 2 back: 6 is in front: no change Inversions: 4 Inversions: 9 Merge. Whenever a back item is pushed up, count how many it goes past
29. 4 6 7 3 5 8 Inversion Counting (Merge Sort) front: 1 2 back: 7 is in front: no change Inversions: 4 Inversions: 9 Merge. Whenever a back item is pushed up, count how many it goes past
30. 4 6 7 3 5 8 Inversion Counting (Merge Sort) 1 2 back: 8 just moves out Inversions: 4 Inversions: 9 Merge. Whenever a back item is pushed up, count how many it goes past
31. 4 6 7 3 5 8 Inversion Counting (Merge Sort) 1 2 Inversions: 4 Inversions: 9 Merge. Whenever a back item is pushed up, count how many it goes past
32. merge-and-count(A,B) ; A,B two input lists (sorted) ; C output list ; i,j current pointers to each list, start at beginning ; a_i, b_j elements pointed by i, j ; count number of inversion, initially 0 while A,B != empty copy min(a_i,b_j) to C if b_j < a_i count += number of element remaining in A j++ else i++ ; now one list is empty copy the remainder of the list to C return count, C Inversion Counting (Merge Sort)
33. sort-and-count(L) if L become less then 2 return 0 else divide L into A, B copy first to mid to A copy from mid to last to B (rA, A) = sort-and-count(A) (rB, B) = sort-and-count(B) (r, L) = merge-and-count(A,B) return r = rA+rB+r, L Inversion Counting (Merge Sort)
34. Related Problem (UVA online-judge) Sorting 299 612 10327 11462 Record based Sorting 482 10194 11804 Inversion Counting 299 10810 11495
35. References Art of Programming Contest by Ahmed ShamsulArefin http://en.wikipedia.org/wiki/Sorting_algorithm