Presentation

731 views

Published on

Sorting and finding the inversion counting

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
731
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Presentation

  1. 1. Sorting <br />Prepared by Anis<br />
  2. 2. Outline<br /><ul><li>Overview on Algorithm
  3. 3. Sorting
  4. 4. Types Of Sorting
  5. 5. Using Predefined Header File
  6. 6. Inversion Counting (Merge Sort)
  7. 7. Related Problem (UVA online-judge)
  8. 8. References</li></li></ul><li>Overview on Algorithm<br /><ul><li>Computing devices are dumb !!!!!
  9. 9. How to solve a problem ????</li></ul> “a small, basic set of primitive instructions”.<br /><ul><li>Complexity of Solving Problems.</li></li></ul><li>Goals of Algorithm Study<br /><ul><li>To develop framework for instructing computer to perform tasks.
  10. 10. To introduce notion of algorithm as means of specifying how to solve a problem.
  11. 11. To introduce and appreciate approaches for defining and solving very complex tasks in terms of simpler tasks.</li></li></ul><li>Sorting<br />Definition of Sorting<br />Sorting makes life easy for searching, eg. Binary Search<br />Things to be considered for Sorting<br />Size / Memory<br />Stability<br />Method<br />Performance Measure<br />
  12. 12. Types Of Sorting<br />Bubble Sort<br /> Speed: O(n^2), extremely slow but stable sorting algorithm<br /> Space: The size of initial array <br /> Coding Complexity: Simple<br />Quick Sort<br /> Speed: O(nlogn), one of the best sorting algorithm for unsorted data<br /> Space: The size of initial array <br /> Coding Complexity: Complex, Divide and Conquer<br />Merge Sort<br /> Speed: O(nlogn) , one of the best stable sorting algorithm <br />Space: The size of initial array <br /> Coding Complexity: Complex, Merging<br />Heap Sort<br /> Speed: O(nlogn) , one of the best unstable sorting algorithm <br /> Space: The size of linked list used<br /> Coding Complexity: Complex, Uses Tree Data structure<br />
  13. 13. Using Predefined Header File<br />Sometimes it is needed to reduce the hassle of recoding same thing over and over again.<br />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.<br />Quick Sort header for C++ Language.<br />qsort(<arrayname>,<size>,sizeof(<elementsize>),compare_function);<br />
  14. 14. Compare function<br />intcompare_function(const void *a,const void *b) <br /> { <br /> // The comparison Code goes here. <br /> }<br />Return type of the compare_function<br />1. negative, if a comes before b <br />Ž 2. 0, if a == b <br />Ž 3. positive, if a comes after b<br /> Using Predefined Header File<br />
  15. 15. Using Predefined Header File<br />Comparing a list of integers <br /> 1. simply cast a and b to integers <br /> 2. x < y, x-y is negative<br /> x == y, x-y is zero <br /> x > y, x-y is positive<br />
  16. 16. Using Predefined Header File<br />Comparing a list of strings<br />Comparing floating point numbers <br />
  17. 17. Using Predefined Header File<br />Multi field sorting<br />Before Sorting<br />After Sorting<br />
  18. 18. Using Predefined Header File<br />
  19. 19. Inversion Counting (Merge Sort)<br />
  20. 20. Inversion Counting (Merge Sort)<br />4<br />7<br />6<br />2<br />1<br />5<br />8<br />3<br />Divide into front and back halves<br />4<br />7<br />6<br />2<br />1<br />5<br />8<br />3<br />4<br />3<br />2<br />4<br />6<br />7<br />1<br />3<br />5<br />8<br />Count inversions within each half, and sort each half<br />Then, count inversions between front and back half<br />
  21. 21. Inversion Counting (Merge Sort)<br />2<br />4<br />6<br />7<br />front:<br />1<br />3<br />5<br />8<br />back:<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  22. 22. Inversion Counting (Merge Sort)<br />2<br />4<br />6<br />7<br />front:<br />1<br />3<br />5<br />8<br />back:<br />skips over 2, 4, 6 and 7<br />Inversions: 4<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  23. 23. Inversion Counting (Merge Sort)<br />2<br />4<br />6<br />7<br />front:<br />1<br />3<br />5<br />8<br />back:<br />from front, nothing to count<br />Inversions: 4<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  24. 24. Inversion Counting (Merge Sort)<br />4<br />6<br />7<br />front:<br />1<br />2<br />3<br />5<br />8<br />back:<br />from front, nothing to count<br />Inversions: 4<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  25. 25. 4<br />6<br />7<br />3<br />5<br />8<br />Inversion Counting (Merge Sort)<br />front:<br />1<br />2<br />back:<br />3 skips over 4, 6, and 7<br />Inversions: 4<br />Inversions: 7<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  26. 26. 4<br />6<br />7<br />3<br />5<br />8<br />Inversion Counting (Merge Sort)<br />front:<br />1<br />2<br />back:<br />4 is in front: no change<br />Inversions: 4<br />Inversions: 7<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  27. 27. 4<br />6<br />7<br />3<br />5<br />8<br />Inversion Counting (Merge Sort)<br />front:<br />1<br />2<br />back:<br />5 moves past 6 and 7<br />Inversions: 4<br />Inversions: 7<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  28. 28. 4<br />6<br />7<br />3<br />5<br />8<br />Inversion Counting (Merge Sort)<br />front:<br />1<br />2<br />back:<br />6 is in front: no change<br />Inversions: 4<br />Inversions: 9<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  29. 29. 4<br />6<br />7<br />3<br />5<br />8<br />Inversion Counting (Merge Sort)<br />front:<br />1<br />2<br />back:<br />7 is in front: no change<br />Inversions: 4<br />Inversions: 9<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  30. 30. 4<br />6<br />7<br />3<br />5<br />8<br />Inversion Counting (Merge Sort)<br />1<br />2<br />back:<br />8 just moves out<br />Inversions: 4<br />Inversions: 9<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  31. 31. 4<br />6<br />7<br />3<br />5<br />8<br />Inversion Counting (Merge Sort)<br />1<br />2<br />Inversions: 4<br />Inversions: 9<br />Merge. Whenever a back item is pushed up, count how many it goes past<br />
  32. 32. merge-and-count(A,B)<br /> ; A,B two input lists (sorted)<br /> ; C output list<br /> ; i,j current pointers to each list, start at beginning<br /> ; a_i, b_j elements pointed by i, j<br /> ; count number of inversion, initially 0<br /> while A,B != empty<br /> copy min(a_i,b_j) to C<br /> if b_j < a_i<br /> count += number of element remaining in A<br /> j++<br /> else<br />i++<br /> ; now one list is empty<br /> copy the remainder of the list to C<br /> return count, C<br />Inversion Counting (Merge Sort)<br />
  33. 33. sort-and-count(L)<br /> if L become less then 2 <br /> return 0<br /> else<br /> divide L into A, B <br /> copy first to mid to A copy from mid to last to B<br /> (rA, A) = sort-and-count(A)<br /> (rB, B) = sort-and-count(B)<br /> (r, L) = merge-and-count(A,B)<br /> return r = rA+rB+r, L<br />Inversion Counting (Merge Sort)<br />
  34. 34. Related Problem (UVA online-judge)<br />Sorting<br />299<br />612<br />10327<br />11462<br />Record based Sorting<br />482<br />10194<br />11804<br />Inversion Counting<br />299<br />10810<br />11495<br />
  35. 35. References<br />Art of Programming Contest by Ahmed ShamsulArefin<br />http://en.wikipedia.org/wiki/Sorting_algorithm<br />
  36. 36. While(1)<br /> { <br /> if(any questions pops)<br /> { <br />printf(“Shoutn”);<br /> process();<br /> }<br /> else<br /> break;<br /> }<br />
  37. 37. THANK YOU<br />

×