SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Sorting  Prepared by Anis
Outline ,[object Object]
Sorting
Types Of Sorting
    Using Predefined Header File
   Inversion Counting (Merge Sort)
Related Problem (UVA online-judge)
   References,[object Object]
How to solve a problem ???? “a small, basic set of primitive instructions”. ,[object Object],[object Object]
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.,[object Object]
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
 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);
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
 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
 Using Predefined Header File Comparing a list of strings Comparing floating point numbers
 Using Predefined Header File Multi field sorting Before Sorting After Sorting
 Using Predefined Header File
Inversion Counting (Merge Sort)
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
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
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
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
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
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
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
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
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
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
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
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

Weitere Àhnliche Inhalte

Was ist angesagt?

(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
ryokollll
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
despicable me
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
Aakash deep Singhal
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 

Was ist angesagt? (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Sorting
SortingSorting
Sorting
 
SORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortSORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix Sort
 
Sorting
SortingSorting
Sorting
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Merge sort
Merge sortMerge sort
Merge sort
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
Counting sort
Counting sortCounting sort
Counting sort
 
6.queue
6.queue6.queue
6.queue
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 

Andere mochten auch

Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
irdginfo
 

Andere mochten auch (14)

Algoritmo Counting sort
Algoritmo Counting sortAlgoritmo Counting sort
Algoritmo Counting sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Distributed Coordination
Distributed CoordinationDistributed Coordination
Distributed Coordination
 
Computer notes - Sorting
Computer notes  - SortingComputer notes  - Sorting
Computer notes - Sorting
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Organisational designs and structures, traditional & contemporary organisatio...
Organisational designs and structures, traditional & contemporary organisatio...Organisational designs and structures, traditional & contemporary organisatio...
Organisational designs and structures, traditional & contemporary organisatio...
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 

Ähnlich wie Presentation

Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
lecture 9
lecture 9lecture 9
lecture 9
sajinsc
 

Ähnlich wie Presentation (20)

Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Sorting2
Sorting2Sorting2
Sorting2
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Analysis of algorithms
Analysis of algorithms Analysis of algorithms
Analysis of algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
lecture 9
lecture 9lecture 9
lecture 9
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
 
L1 - Recap.pdf
L1 - Recap.pdfL1 - Recap.pdf
L1 - Recap.pdf
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Parallel search
Parallel searchParallel search
Parallel search
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 

KĂŒrzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

KĂŒrzlich hochgeladen (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

Presentation

  • 2.
  • 5. Using Predefined Header File
  • 6. Inversion Counting (Merge Sort)
  • 7. Related Problem (UVA online-judge)
  • 8.
  • 9.
  • 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
  • 18. Using Predefined Header File
  • 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
  • 36. While(1) { if(any questions pops) { printf(“Shout”); process(); } else break; }