Sorting Prepared by Anis
OutlineOverview on Algorithm
Sorting
Types Of Sorting
    Using Predefined Header File
   Inversion Counting (Merge Sort)
Related Problem (UVA online-judge)
   ReferencesOverview on AlgorithmComputing devices are dumb !!!!!
How to solve a problem ???? “a small, basic set of primitive instructions”.Complexity of Solving Problems.Goals of Algorithm StudyTo develop framework for instructing computer to perform tasks.
To introduce notion of algorithm as means of specifying how to solve a problem.
To introduce and appreciate approaches for defining and solving very complex tasks in terms of simpler tasks.SortingDefinition of SortingSorting makes life easy for searching, eg. Binary SearchThings to be considered for SortingSize / MemoryStabilityMethodPerformance Measure
Types Of SortingBubble Sort                Speed: O(n^2), extremely slow but stable sorting algorithm                Space: The size of initial array                Coding Complexity: SimpleQuick Sort                Speed: O(nlogn), one of the best sorting algorithm  for unsorted data                Space: The size of initial array                 Coding Complexity: Complex, Divide and ConquerMerge Sort                Speed: O(nlogn) , one of the best stable sorting algorithm Space: The size of initial array                 Coding Complexity: Complex, MergingHeap 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
 Using Predefined Header FileSometimes 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);
Compare functionintcompare_function(const void *a,const void *b)    {             // The comparison Code goes here.     }Return type of the compare_function1. negative, if a comes before b Ž                 2. 0,             if a == b Ž                 3. positive, if a comes after b Using Predefined Header File
 Using Predefined Header FileComparing 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
 Using Predefined Header FileComparing a list of stringsComparing floating point numbers
 Using Predefined Header FileMulti field sortingBefore SortingAfter Sorting
 Using Predefined Header File
Inversion Counting (Merge Sort)
Inversion Counting (Merge Sort)47621583Divide into front and back halves476215834324671358Count inversions within each half, and sort each halfThen, count inversions between front and back half
Inversion Counting (Merge Sort)2467front:1358back:Merge.  Whenever a back item is pushed up, count how many it goes past
Inversion Counting (Merge Sort)2467front:1358back:skips over 2, 4, 6 and 7Inversions: 4Merge.  Whenever a back item is pushed up, count how many it goes past
Inversion Counting (Merge Sort)2467front:1358back:from front, nothing to countInversions: 4Merge.  Whenever a back item is pushed up, count how many it goes past
Inversion Counting (Merge Sort)467front:12358back:from front, nothing to countInversions: 4Merge.  Whenever a back item is pushed up, count how many it goes past
467358Inversion Counting (Merge Sort)front:12back:3 skips over 4, 6, and 7Inversions: 4Inversions: 7Merge.  Whenever a back item is pushed up, count how many it goes past
467358Inversion Counting (Merge Sort)front:12back:4 is in front: no changeInversions: 4Inversions: 7Merge.  Whenever a back item is pushed up, count how many it goes past
467358Inversion Counting (Merge Sort)front:12back:5 moves past 6 and 7Inversions: 4Inversions: 7Merge.  Whenever a back item is pushed up, count how many it goes past
467358Inversion Counting (Merge Sort)front:12back:6 is in front: no changeInversions: 4Inversions: 9Merge.  Whenever a back item is pushed up, count how many it goes past
467358Inversion Counting (Merge Sort)front:12back:7 is in front: no changeInversions: 4Inversions: 9Merge.  Whenever a back item is pushed up, count how many it goes past
467358Inversion Counting (Merge Sort)12back:8 just moves outInversions: 4Inversions: 9Merge.  Whenever a back item is pushed up, count how many it goes past
467358Inversion Counting (Merge Sort)12Inversions: 4Inversions: 9Merge.  Whenever a back item is pushed up, count how many it goes past

Presentation

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
    Using Predefined Header File
  • 6.
    Inversion Counting (Merge Sort)
  • 7.
  • 8.
    ReferencesOverview on AlgorithmComputing devices are dumb !!!!!
  • 9.
    How to solvea problem ???? “a small, basic set of primitive instructions”.Complexity of Solving Problems.Goals of Algorithm StudyTo develop framework for instructing computer to perform tasks.
  • 10.
    To introduce notionof algorithm as means of specifying how to solve a problem.
  • 11.
    To introduce andappreciate approaches for defining and solving very complex tasks in terms of simpler tasks.SortingDefinition of SortingSorting makes life easy for searching, eg. Binary SearchThings to be considered for SortingSize / MemoryStabilityMethodPerformance Measure
  • 12.
    Types Of SortingBubbleSort Speed: O(n^2), extremely slow but stable sorting algorithm Space: The size of initial array Coding Complexity: SimpleQuick Sort Speed: O(nlogn), one of the best sorting algorithm for unsorted data Space: The size of initial array Coding Complexity: Complex, Divide and ConquerMerge Sort Speed: O(nlogn) , one of the best stable sorting algorithm Space: The size of initial array Coding Complexity: Complex, MergingHeap 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 PredefinedHeader FileSometimes 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 functionintcompare_function(const void*a,const void *b) { // The comparison Code goes here. }Return type of the compare_function1. negative, if a comes before b Ž 2. 0, if a == b Ž 3. positive, if a comes after b Using Predefined Header File
  • 15.
    Using PredefinedHeader FileComparing 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 PredefinedHeader FileComparing a list of stringsComparing floating point numbers
  • 17.
    Using PredefinedHeader FileMulti field sortingBefore SortingAfter Sorting
  • 18.
  • 19.
  • 20.
    Inversion Counting (MergeSort)47621583Divide into front and back halves476215834324671358Count inversions within each half, and sort each halfThen, count inversions between front and back half
  • 21.
    Inversion Counting (MergeSort)2467front:1358back:Merge. Whenever a back item is pushed up, count how many it goes past
  • 22.
    Inversion Counting (MergeSort)2467front:1358back:skips over 2, 4, 6 and 7Inversions: 4Merge. Whenever a back item is pushed up, count how many it goes past
  • 23.
    Inversion Counting (MergeSort)2467front:1358back:from front, nothing to countInversions: 4Merge. Whenever a back item is pushed up, count how many it goes past
  • 24.
    Inversion Counting (MergeSort)467front:12358back:from front, nothing to countInversions: 4Merge. Whenever a back item is pushed up, count how many it goes past
  • 25.
    467358Inversion Counting (MergeSort)front:12back:3 skips over 4, 6, and 7Inversions: 4Inversions: 7Merge. Whenever a back item is pushed up, count how many it goes past
  • 26.
    467358Inversion Counting (MergeSort)front:12back:4 is in front: no changeInversions: 4Inversions: 7Merge. Whenever a back item is pushed up, count how many it goes past
  • 27.
    467358Inversion Counting (MergeSort)front:12back:5 moves past 6 and 7Inversions: 4Inversions: 7Merge. Whenever a back item is pushed up, count how many it goes past
  • 28.
    467358Inversion Counting (MergeSort)front:12back:6 is in front: no changeInversions: 4Inversions: 9Merge. Whenever a back item is pushed up, count how many it goes past
  • 29.
    467358Inversion Counting (MergeSort)front:12back:7 is in front: no changeInversions: 4Inversions: 9Merge. Whenever a back item is pushed up, count how many it goes past
  • 30.
    467358Inversion Counting (MergeSort)12back:8 just moves outInversions: 4Inversions: 9Merge. Whenever a back item is pushed up, count how many it goes past
  • 31.
    467358Inversion Counting (MergeSort)12Inversions: 4Inversions: 9Merge. Whenever a back item is pushed up, count how many it goes past