SlideShare a Scribd company logo
Class No.39  Data Structures http://ecomputernotes.com
Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 http://ecomputernotes.com 10 12 8 4 2 11 7 5
Divide and Conquer Sort the two parts: http://ecomputernotes.com 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11
Divide and Conquer Then merge the two parts together: http://ecomputernotes.com 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12
Analysis To sort the halves    ( n /2) 2 +( n /2) 2 To merge the two halves     n   So, for  n =100, divide and conquer takes: = (100/2) 2  + (100/2) 2  + 100 = 2500 + 2500 + 100  = 5100   ( n 2  = 10,000)  http://ecomputernotes.com
Divide and Conquer Why not divide the halves in half? The quarters in half? And so on . . . When should we stop? At  n  = 1 http://ecomputernotes.com
Search Divide and Conquer Recall:  Binary Search http://ecomputernotes.com Search Search
Sort Divide and Conquer Sort Sort Sort Sort Sort Sort http://ecomputernotes.com
Divide and Conquer Combine Combine Combine http://ecomputernotes.com
Mergesort Mergesort  is a  divide and conquer  algorithm that does exactly that. It splits the list in half Mergesorts the two halves Then merges the two sorted halves together Mergesort can be implemented  recursively http://ecomputernotes.com
Mergesort The mergesort algorithm involves three steps: If the number of items to sort is 0 or 1, return Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 4 http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 4 5 http://ecomputernotes.com
Merging 4 8 10 12 2 5 7 11 2 4 5 7 http://ecomputernotes.com
Mergesort Split the list in half. Mergesort the left half. Split the list in half. Mergesort the left half. Split the list in half. Mergesort the left half. 10 Mergesort the right. 4 http://ecomputernotes.com 8 12 11 2 7 5 4 10 8 12 4 10 4 10
Mergesort 8 12 4 10 Mergesort the right half. Merge the two halves. 12 8 http://ecomputernotes.com 8 12 11 2 7 5 4 10 4 10 10 4 8 12 Merge the two halves . 8 8 12
Mergesort 8 12 4 10 Merge the two halves. Mergesort the right half. Merge the two halves. http://ecomputernotes.com 8 12 11 2 7 5 4 10 4 10 10 4 8 12 10 12 8 4 10 4 8 12
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 http://ecomputernotes.com 11 2 7 5 11 2
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 http://ecomputernotes.com 2 11
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11 http://ecomputernotes.com
Mergesort 10 12 2 5 7 11 8 4 Mergesort the right half. http://ecomputernotes.com
Mergesort 5 7 8 10 11 12 4 2 Merge the two halves. http://ecomputernotes.com
void mergeSort(float array[], int size) { int* tmpArrayPtr = new int[size];  if (tmpArrayPtr != NULL)  mergeSortRec(array, size, tmpArrayPtr); else { cout << “Not enough memory to sort list.\n”); return; } delete [] tmpArrayPtr; } Mergesort http://ecomputernotes.com
void mergeSortRec(int array[],int size,int tmp[]) { int  i; int  mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid,  tmp);  for (i = 0; i < size; i++)  array[i] = tmp[i]; } } Mergesort http://ecomputernotes.com
3 5 15 28 30 6 10 14 22 43 50 a: b: mergeArrays http://ecomputernotes.com aSize: 5 bSize: 6 tmp:
mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 j=0 3 6 http://ecomputernotes.com
mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 3 j=0 3 6 http://ecomputernotes.com
mergeArrays 3 15 28 30 10 14 22 43 50 a: b: tmp: i=1 j=0 k=1 3 5 5 6 http://ecomputernotes.com
mergeArrays 3 5 28 30 10 14 22 43 50 a: b: 3 5 tmp: i=2 j=0 k=2 6 15 6 http://ecomputernotes.com
mergeArrays 3 5 28 30 6 14 22 43 50 a: b: 3 5 6 tmp: i=2 j=1 k=3 15 10 10 http://ecomputernotes.com
mergeArrays 10 3 5 28 30 6 22 43 50 a: b: 3 5 6 tmp: i=2 j=2 k=4 15 10 14 14 http://ecomputernotes.com
mergeArrays 14 10 3 5 28 30 6 14 43 50 a: b: 3 5 6 tmp: i=2 j=3 k=5 15 10 22 15 http://ecomputernotes.com
mergeArrays 14 10 3 5 30 6 14 43 50 a: b: 3 5 6 tmp: i=3 j=3 k=6 15 10 22 22 15 28 http://ecomputernotes.com
mergeArrays 14 10 3 5 30 6 14 50 a: b: 3 5 6 tmp: i=3 j=4 k=7 15 10 22 28 15 28 43 22 http://ecomputernotes.com
mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 tmp: i=4 j=4 k=8 15 10 22 30 15 28 43 22 30 28 http://ecomputernotes.com
mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 30 tmp: i=5 j=4 k=9 15 10 22 15 28 43 22 30 28 43 50 Done. http://ecomputernotes.com
Merge Sort and Linked Lists http://ecomputernotes.com Sort Sort Merge
Mergesort Analysis Merging the two lists of size  n /2: O( n ) Merging the four lists of size n/4: O( n ) . . . Merging the n lists of size 1: O( n ) O (lg  n ) times Mergesort is O( n  lg  n ) Space? The other sorts we have looked at (insertion, selection) are  in-place  (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging
Mergesort Analysis Mergesort is O( n  lg  n ) Space? The other sorts we have looked at (insertion, selection) are  in-place  (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging http://ecomputernotes.com
Quicksort Quicksort   is another divide and conquer algorithm Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value http://ecomputernotes.com
Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or  middle  of list): 8 3 2 11 7 5 12 4 5 http://ecomputernotes.com 4 10 5 pivot value
Quicksort The pivot is swapped to the last position and the  remaining elements are compared starting at the ends. 8 3 2 11 7 5 12 4 5 5 pivot value http://ecomputernotes.com 4 10 low high
Quicksort Then the low index moves right until it is at an element  that is larger than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 10 12 4 6 5 pivot value 3 12 http://ecomputernotes.com low high
Quicksort Then the high index moves left until it is at an  element that is smaller than the pivot value (i.e., it  is on the wrong side) 8 6 2 11 7 5 12 4 6 5 pivot value 3 2 http://ecomputernotes.com 4 10 low high
Quicksort Then the two values are swapped and the index values are updated: 8 6 2 11 7 5 12 4 6 5 pivot value 3 2 12 http://ecomputernotes.com 4 10 low high
Quicksort This continues until the two index values pass  each other: 8 6 12 11 7 5 4 2 4 6 5 pivot value 3 10 3 10 http://ecomputernotes.com low high
Quicksort This continues until the two index values pass  each other: 8 6 12 11 7 5 4 2 4 6 5 pivot value 10 3 http://ecomputernotes.com low high
Quicksort Then the pivot value is swapped into position: 8 6 12 11 7 5 4 2 4 6 10 3 8 5 http://ecomputernotes.com low high
Quicksort Recursively quicksort the two parts: 5 6 12 11 7 8 4 2 4 6 10 3 5 http://ecomputernotes.com Quicksort the left part Quicksort the right part
Quicksort void quickSort(int array[], int size) { int index; if (size > 1) { index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } } http://ecomputernotes.com
int partition(int array[], int size) { int k; int mid = size/2; int index = 0; swap(array, array+mid);  for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } } swap(array, array+index); return index; } Quicksort
Data Structures-Course Recap Arrays Link Lists Stacks Queues Binary Trees Sorting http://ecomputernotes.com

More Related Content

What's hot (6)

PPT
computer notes - Data Structures - 5
ecomputernotes
 
PPTX
Heap sort
zahraa F.Muhsen
 
PPT
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
PDF
Working of Merge Sort Code
Muhammad Abdullah
 
PPT
chapter - 6.ppt
Tareq Hasan
 
PPT
Heap sort
Mohd Arif
 
computer notes - Data Structures - 5
ecomputernotes
 
Heap sort
zahraa F.Muhsen
 
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Working of Merge Sort Code
Muhammad Abdullah
 
chapter - 6.ppt
Tareq Hasan
 
Heap sort
Mohd Arif
 

Similar to Computer notes - Mergesort (20)

PPT
Lec 6 Divide and conquer of Data Structures & Algortihms
haseebanjum2611
 
PDF
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
PPTX
MergeSort in data structure and its applications.pptx
ananya195642
 
PPT
Sorting algos > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
PPTX
data structures and algorithms Unit 3
infanciaj
 
PPTX
Mergesort
SimoniShah6
 
PPT
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
PDF
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
PPTX
Merge sort-algorithm for computer science engineering students
University of Science and Technology Chitttagong
 
PPT
Tri Merge Sorting Algorithm
Ashim Sikder
 
PPTX
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PDF
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
mdameer02
 
PPT
Lec35
Nikhil Chilwant
 
PPT
Data Structures 7
Dr.Umadevi V
 
PPTX
ch16.pptx
lordaragorn2
 
PPTX
ch16 (1).pptx
lordaragorn2
 
Lec 6 Divide and conquer of Data Structures & Algortihms
haseebanjum2611
 
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
MergeSort in data structure and its applications.pptx
ananya195642
 
Sorting algos > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
data structures and algorithms Unit 3
infanciaj
 
Mergesort
SimoniShah6
 
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
Merge sort-algorithm for computer science engineering students
University of Science and Technology Chitttagong
 
Tri Merge Sorting Algorithm
Ashim Sikder
 
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
mdameer02
 
Data Structures 7
Dr.Umadevi V
 
ch16.pptx
lordaragorn2
 
ch16 (1).pptx
lordaragorn2
 
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 11
ecomputernotes
 
PPT
computer notes - Data Structures - 20
ecomputernotes
 
PPT
computer notes - Data Structures - 15
ecomputernotes
 
DOC
Computer notes - Including Constraints
ecomputernotes
 
DOC
Computer notes - Date time Functions
ecomputernotes
 
DOC
Computer notes - Subqueries
ecomputernotes
 
DOC
Computer notes - Other Database Objects
ecomputernotes
 
PPT
computer notes - Data Structures - 28
ecomputernotes
 
PPT
computer notes - Data Structures - 19
ecomputernotes
 
PPT
computer notes - Data Structures - 31
ecomputernotes
 
PPT
computer notes - Data Structures - 4
ecomputernotes
 
PPT
computer notes - Data Structures - 13
ecomputernotes
 
DOC
Computer notes - Advanced Subqueries
ecomputernotes
 
DOC
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
PPT
computer notes - Data Structures - 16
ecomputernotes
 
PPT
computer notes - Data Structures - 22
ecomputernotes
 
PPT
computer notes - Data Structures - 35
ecomputernotes
 
PPT
computer notes - Data Structures - 36
ecomputernotes
 
DOC
Computer notes - Enhancements to the GROUP BY Clause
ecomputernotes
 
DOC
Computer notes - Manipulating Data
ecomputernotes
 
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Including Constraints
ecomputernotes
 
Computer notes - Date time Functions
ecomputernotes
 
Computer notes - Subqueries
ecomputernotes
 
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 31
ecomputernotes
 
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
ecomputernotes
 
Computer notes - Manipulating Data
ecomputernotes
 
Ad

Recently uploaded (20)

PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Horarios de distribución de agua en julio
pegazohn1978
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 

Computer notes - Mergesort

  • 1. Class No.39 Data Structures http://ecomputernotes.com
  • 2. Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 http://ecomputernotes.com 10 12 8 4 2 11 7 5
  • 3. Divide and Conquer Sort the two parts: http://ecomputernotes.com 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11
  • 4. Divide and Conquer Then merge the two parts together: http://ecomputernotes.com 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12
  • 5. Analysis To sort the halves  ( n /2) 2 +( n /2) 2 To merge the two halves  n So, for n =100, divide and conquer takes: = (100/2) 2 + (100/2) 2 + 100 = 2500 + 2500 + 100 = 5100 ( n 2 = 10,000) http://ecomputernotes.com
  • 6. Divide and Conquer Why not divide the halves in half? The quarters in half? And so on . . . When should we stop? At n = 1 http://ecomputernotes.com
  • 7. Search Divide and Conquer Recall: Binary Search http://ecomputernotes.com Search Search
  • 8. Sort Divide and Conquer Sort Sort Sort Sort Sort Sort http://ecomputernotes.com
  • 9. Divide and Conquer Combine Combine Combine http://ecomputernotes.com
  • 10. Mergesort Mergesort is a divide and conquer algorithm that does exactly that. It splits the list in half Mergesorts the two halves Then merges the two sorted halves together Mergesort can be implemented recursively http://ecomputernotes.com
  • 11. Mergesort The mergesort algorithm involves three steps: If the number of items to sort is 0 or 1, return Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group http://ecomputernotes.com
  • 12. Merging: animation 4 8 10 12 2 5 7 11 2 http://ecomputernotes.com
  • 13. Merging: animation 4 8 10 12 2 5 7 11 2 4 http://ecomputernotes.com
  • 14. Merging: animation 4 8 10 12 2 5 7 11 2 4 5 http://ecomputernotes.com
  • 15. Merging 4 8 10 12 2 5 7 11 2 4 5 7 http://ecomputernotes.com
  • 16. Mergesort Split the list in half. Mergesort the left half. Split the list in half. Mergesort the left half. Split the list in half. Mergesort the left half. 10 Mergesort the right. 4 http://ecomputernotes.com 8 12 11 2 7 5 4 10 8 12 4 10 4 10
  • 17. Mergesort 8 12 4 10 Mergesort the right half. Merge the two halves. 12 8 http://ecomputernotes.com 8 12 11 2 7 5 4 10 4 10 10 4 8 12 Merge the two halves . 8 8 12
  • 18. Mergesort 8 12 4 10 Merge the two halves. Mergesort the right half. Merge the two halves. http://ecomputernotes.com 8 12 11 2 7 5 4 10 4 10 10 4 8 12 10 12 8 4 10 4 8 12
  • 19. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 http://ecomputernotes.com 11 2 7 5 11 2
  • 20. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 http://ecomputernotes.com 2 11
  • 21. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5 http://ecomputernotes.com
  • 22. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11 http://ecomputernotes.com
  • 23. Mergesort 10 12 2 5 7 11 8 4 Mergesort the right half. http://ecomputernotes.com
  • 24. Mergesort 5 7 8 10 11 12 4 2 Merge the two halves. http://ecomputernotes.com
  • 25. void mergeSort(float array[], int size) { int* tmpArrayPtr = new int[size]; if (tmpArrayPtr != NULL) mergeSortRec(array, size, tmpArrayPtr); else { cout << “Not enough memory to sort list.\n”); return; } delete [] tmpArrayPtr; } Mergesort http://ecomputernotes.com
  • 26. void mergeSortRec(int array[],int size,int tmp[]) { int i; int mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid, tmp); for (i = 0; i < size; i++) array[i] = tmp[i]; } } Mergesort http://ecomputernotes.com
  • 27. 3 5 15 28 30 6 10 14 22 43 50 a: b: mergeArrays http://ecomputernotes.com aSize: 5 bSize: 6 tmp:
  • 28. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 j=0 3 6 http://ecomputernotes.com
  • 29. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 3 j=0 3 6 http://ecomputernotes.com
  • 30. mergeArrays 3 15 28 30 10 14 22 43 50 a: b: tmp: i=1 j=0 k=1 3 5 5 6 http://ecomputernotes.com
  • 31. mergeArrays 3 5 28 30 10 14 22 43 50 a: b: 3 5 tmp: i=2 j=0 k=2 6 15 6 http://ecomputernotes.com
  • 32. mergeArrays 3 5 28 30 6 14 22 43 50 a: b: 3 5 6 tmp: i=2 j=1 k=3 15 10 10 http://ecomputernotes.com
  • 33. mergeArrays 10 3 5 28 30 6 22 43 50 a: b: 3 5 6 tmp: i=2 j=2 k=4 15 10 14 14 http://ecomputernotes.com
  • 34. mergeArrays 14 10 3 5 28 30 6 14 43 50 a: b: 3 5 6 tmp: i=2 j=3 k=5 15 10 22 15 http://ecomputernotes.com
  • 35. mergeArrays 14 10 3 5 30 6 14 43 50 a: b: 3 5 6 tmp: i=3 j=3 k=6 15 10 22 22 15 28 http://ecomputernotes.com
  • 36. mergeArrays 14 10 3 5 30 6 14 50 a: b: 3 5 6 tmp: i=3 j=4 k=7 15 10 22 28 15 28 43 22 http://ecomputernotes.com
  • 37. mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 tmp: i=4 j=4 k=8 15 10 22 30 15 28 43 22 30 28 http://ecomputernotes.com
  • 38. mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 30 tmp: i=5 j=4 k=9 15 10 22 15 28 43 22 30 28 43 50 Done. http://ecomputernotes.com
  • 39. Merge Sort and Linked Lists http://ecomputernotes.com Sort Sort Merge
  • 40. Mergesort Analysis Merging the two lists of size n /2: O( n ) Merging the four lists of size n/4: O( n ) . . . Merging the n lists of size 1: O( n ) O (lg n ) times Mergesort is O( n lg n ) Space? The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging
  • 41. Mergesort Analysis Mergesort is O( n lg n ) Space? The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging http://ecomputernotes.com
  • 42. Quicksort Quicksort is another divide and conquer algorithm Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value http://ecomputernotes.com
  • 43. Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or middle of list): 8 3 2 11 7 5 12 4 5 http://ecomputernotes.com 4 10 5 pivot value
  • 44. Quicksort The pivot is swapped to the last position and the remaining elements are compared starting at the ends. 8 3 2 11 7 5 12 4 5 5 pivot value http://ecomputernotes.com 4 10 low high
  • 45. Quicksort Then the low index moves right until it is at an element that is larger than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 10 12 4 6 5 pivot value 3 12 http://ecomputernotes.com low high
  • 46. Quicksort Then the high index moves left until it is at an element that is smaller than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 12 4 6 5 pivot value 3 2 http://ecomputernotes.com 4 10 low high
  • 47. Quicksort Then the two values are swapped and the index values are updated: 8 6 2 11 7 5 12 4 6 5 pivot value 3 2 12 http://ecomputernotes.com 4 10 low high
  • 48. Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 5 pivot value 3 10 3 10 http://ecomputernotes.com low high
  • 49. Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 5 pivot value 10 3 http://ecomputernotes.com low high
  • 50. Quicksort Then the pivot value is swapped into position: 8 6 12 11 7 5 4 2 4 6 10 3 8 5 http://ecomputernotes.com low high
  • 51. Quicksort Recursively quicksort the two parts: 5 6 12 11 7 8 4 2 4 6 10 3 5 http://ecomputernotes.com Quicksort the left part Quicksort the right part
  • 52. Quicksort void quickSort(int array[], int size) { int index; if (size > 1) { index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } } http://ecomputernotes.com
  • 53. int partition(int array[], int size) { int k; int mid = size/2; int index = 0; swap(array, array+mid); for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } } swap(array, array+index); return index; } Quicksort
  • 54. Data Structures-Course Recap Arrays Link Lists Stacks Queues Binary Trees Sorting http://ecomputernotes.com

Editor's Notes

  • #5: End of lecture 44.
  • #6: Start of lecture 45
  • #53: End of lecture 45. At last!