joseph lindo sorting algorithms sir joseph lindo university of the cordilleras
TRANSCRIPT
![Page 1: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/1.jpg)
Joseph Lindo
SortingAlgorithms
Sir Joseph LindoUniversity of the Cordilleras
![Page 2: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/2.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
SortingSorting
An operation that segregates items into groups according to specified criterion
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
Definition
![Page 3: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/3.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
Sorting
Why sorting?
Examples:
Sorting Books in Library (Dewey system)
Sorting Individuals by Height (Feet and Inches)
Reason
![Page 4: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/4.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
Sorting
Why sorting?
Examples:
Sorting Movies in Blockbuster (Alphabetical)
Sorting Numbers (Sequential)
Reason
![Page 5: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/5.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
Sorting
Why sorting?It is used in a wide range of
applications
Several sorting algorithms have been invented because the task is so fundamental and also frequently used
Reason
![Page 6: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/6.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
SortingSorting
Algorithms
Algorithms
![Page 7: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/7.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
SortingSorting
AlgorithmsBubble SortSelection SortInsertion SortMerge SortQuick Sort
Algorithms
![Page 8: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/8.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
SortingGroup
Case StudyGroup maximum of five
membersShort bond paperCreate the flowchart of:
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Activity
![Page 9: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/9.jpg)
Joseph Lindo
Activity
Algorithms
Definition
Reason
SortingGroup
Case Study Group maximum of five
members Yellow paper Construct your own “Sorting Algorithm” Provide example Provide the CodeActivity
![Page 10: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/10.jpg)
Joseph Lindo
SortingAlgorithms
--end--
Sir Joseph LindoUniversity of the Cordilleras
![Page 11: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/11.jpg)
Joseph Lindo
Insertion
Bubble
Merge
Selection
Quick
Sorting Algorithms
Bubble SortThe simple idea is keep
passing through the list, swapping the next element that is out of order, until the list is sorted.
Bubble
![Page 12: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/12.jpg)
Joseph Lindo
Insertion
Bubble
Merge
Selection
Quick
Sorting Algorithms
Selection SortWorks by selecting
elements in specific order, and putting them in proper position in the final list
It is an in-place sorting algorithm
Selection
![Page 13: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/13.jpg)
Joseph Lindo
Insertion
Bubble
Merge
Selection
Quick
Sorting Algorithms
Insertion SortIt is a comparison based
sort, which sorts the array one entry at a time.Insertion
![Page 14: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/14.jpg)
Joseph Lindo
Insertion
Bubble
Merge
Selection
Quick
Sorting Algorithms
Merge SortOriginal problem is split into
subproblems
Solutions to subproblems lead to the solution of the main problemMerge
![Page 15: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/15.jpg)
Joseph Lindo
Insertion
Bubble
Merge
Selection
Quick
Sorting Algorithms
Quick Sort
Invented by C.A.R. Hoare
Also based on the divide-and-conquer paradigm
Quick
![Page 16: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/16.jpg)
Joseph Lindo
Characteristics
jo
Stable Algorithm
A stable sort keeps equal elements in the same order
This may matter when you are sorting data according to some characteristic
Example: sorting students by test scores
![Page 17: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/17.jpg)
Joseph Lindo
Characteristics
jo
Stable Algorithm
Bob
Ann
Joe
Zöe
Dan
Pat
Sam
90
98
98
86
75
86
90
original array
Bob
Ann
Joe
Zöe
Dan
Pat
Sam
90
98
98
86
75
86
90
stably sorted
![Page 18: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/18.jpg)
Joseph Lindo
Characteristics
Unstable Algorithm
An unstable sort may or may not keep equal elements in the same order
Stability is usually not important, but sometimes it is important
![Page 19: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/19.jpg)
Joseph Lindo
Characteristics
Unstable Algorithm
Bob
Ann
Joe
Zöe
Dan
Pat
Sam
90
98
98
86
75
86
90
original array
Bob
Ann
Joe
Zöe
Dan
Pat
Sam
90
98
98
86
75
86
90
unstably sorted
![Page 20: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/20.jpg)
Joseph Lindo
Characteristics
Bubble Sort
Compare each element (except the last one) with its neighbor to the right
Compare each element (except the last two) with its neighbor to the right
Compare each element (except the last three) with its neighbor to the right
Repeat the process
![Page 21: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/21.jpg)
Joseph Lindo
Diagram
Bubble Sort
7 2 8 5 4
2 7 8 5 4
2 7 8 5 4
2 7 5 8 4
2 7 5 4 8
2 7 5 4 8
2 5 7 4 8
2 5 4 7 8
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 5 4 7 8
2 4 5 7 8
2 4 5 7 8
(done)
![Page 22: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/22.jpg)
Joseph Lindo
Code
Bubble Sort
public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--){ for (inner = 0; inner < outer; inner++){ if (a[inner] > a[inner + 1]) { int temp = a[inner]; a[inner] = a[inner + 1]; a[inner + 1] = temp; } } }}
![Page 23: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/23.jpg)
Joseph Lindo
Characteristics
Selection Sort
Given an array of length n,Search elements 0 through n-1 and select the smallest Swap it with the element in location 0)
Search elements 1 through n-1 and select the smallest Swap it with the element in location 1
Search elements 2 through n-1 and select the smallest Swap it with the element in location 2
Continue in this fashion until there’s nothing left to search
![Page 24: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/24.jpg)
Joseph Lindo
Diagram
Selection Sort
7 2 8 5 4
2 7 8 5 4
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
![Page 25: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/25.jpg)
Joseph Lindo
Code
Selection Sort
public static void selectionSort(int[] a){ int outer, inner, min; for(outer=0;outer<a.length-1;outer++){ min = outer; for(inner=outer+1;inner<a.length;inner++){
if (a[inner] < a[min]){ min = inner; } } int temp = a[outer]; a[outer] = a[min]; a[min] = temp; }}
![Page 26: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/26.jpg)
Joseph Lindo
Characteristics
Insertion Sort
Repeated the following steps until no elements are left in the unsorted part of the array
– First available element is selected from the unsorted section of the array
– Place selected element in its proper position in the sorted section of the array
![Page 27: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/27.jpg)
Joseph Lindo
Diagram
Insertion Sort
27
3 4 7 1214142021333810559232816
sorted next to be inserted
3 4 7 559232816
10temp
3833212014141210
sorted
less than 10
![Page 28: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/28.jpg)
Joseph Lindo
Code
Insertion Sort
void insertionSort(Object array[], int startIdx, int endIdx){ for (int i = startIdx; i < endIdx; i++){ int k = i; for (int j = i + 1; j < endIdx; j++){ if (array[k]>array[j]){ k = j; } int temp = array[k]; array[k] = array[j]; array[j] = temp;
} } }
![Page 29: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/29.jpg)
Joseph Lindo
Characteristics
Merge Sort
Three steps:– Divide
Divide the sequence of data elements into two halves– Conquer
Conquer each half sorting– Combine
Combine or merge the two halves to come up with the sorted sequence
![Page 30: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/30.jpg)
Joseph Lindo
Diagram
Merge Sort
Merge
SORT
Divide intotwo halves
FirstPart SecondPart
FirstPart SecondPart
A
A is sorted!
![Page 31: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/31.jpg)
Joseph Lindo
Diagram
Merge Sort
6 10 14 223 5 15 28
L: R:
Temporary Arrays
5 15 28 30 6 10 1452 3 7 8 1 4 5 6A:
![Page 32: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/32.jpg)
Joseph Lindo
Diagram
Merge Sort
3 5 15 28 30 6 10 14
L:
A:
3 15 28 30 6 10 14 22
R:
i=0 j=0
k=0
2 3 7 8 1 4 5 6
1
![Page 33: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/33.jpg)
Joseph Lindo
Diagram
Merge Sort
1 5 15 28 30 6 10 14
L:
A:
3 5 15 28 6 10 14 22
R:
k=1
2 3 7 8 1 4 5 6
2
i=0 j=1
![Page 34: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/34.jpg)
Joseph Lindo
Diagram
Merge Sort
1 2 15 28 30 6 10 14
L:
A:
6 10 14 22
R:
i=1
k=2
2 3 7 8 1 4 5 6
3
j=1
![Page 35: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/35.jpg)
Joseph Lindo
Diagram
Merge Sort
1 2 3 6 10 14
L:
A:
6 10 14 22
R:
i=2 j=1
k=3
2 3 7 8 1 4 5 6
4
![Page 36: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/36.jpg)
Joseph Lindo
Diagram
Merge Sort
1 2 3 4 6 10 14
L:
A:
6 10 14 22
R:
j=2
k=4
2 3 7 8 1 4 5 6
i=2
5
![Page 37: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/37.jpg)
Joseph Lindo
Diagram
Merge Sort
1 2 3 4 5 6 10 14
L:
A:
6 10 14 22
R:
i=2 j=3
k=5
2 3 7 8 1 4 5 6
6
![Page 38: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/38.jpg)
Joseph Lindo
Diagram
Merge Sort
1 2 3 4 5 6 14
L:
A:
6 10 14 22
R:
k=6
2 3 7 8 1 4 5 6
7
i=2
![Page 39: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/39.jpg)
Joseph Lindo
Diagram
Merge Sort
-1 2 3 4 5 6 7 14
L:
A:
3 5 15 28 6 10 14 22
R:2 3 7 8 1 4 5 6
8
i=3
k=7
![Page 40: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/40.jpg)
Joseph Lindo
Diagram
Merge Sort
1 2 3 4 5 6 7 8
L:
A:
3 5 15 28 6 10 14 22
R:2 3 7 8 1 4 5 6
i=4
k=8
![Page 41: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/41.jpg)
Joseph Lindo
Code
Merge Sort
void mergeSort(Object array[], int startIdx,int endIdx){ if (array.length != 1) { mergeSort(leftArr,startIdx, midIdx); mergeSort(rightArr, midIdx+1,endIdx); combine(leftArr, rightArr); }}
![Page 42: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/42.jpg)
Joseph Lindo
Characteristics
Quick Sort
Divide-and-Conquer Paradigm– Divide
Partition array into two subarrays A[p...q-1] and A[q+1...r] such that each element in A[p...q-1] is less than or equal to A[q] and each element in A[q+1...r] is greater than or equal to A[q]
A[q] is called the pivot– Conquer
Sort the subarrays
![Page 43: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/43.jpg)
Joseph Lindo
Diagram
Quick Sort
x < p p p ≤ x
PartitionFirstPart SecondPart
p
pivot
A:
x < p p p ≤ x
SortedFirstPart
SortedSecondPart
Sorted
![Page 44: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/44.jpg)
Joseph Lindo
Diagram
Quick Sort
p
p x < p p ≤ x
p p ≤ xx < p
A:
A:
A: p
![Page 45: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/45.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 8 6 3 5 1 7 2
![Page 46: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/46.jpg)
Joseph Lindo
Diagram
Quick Sort
4 8 6 3 5 1 7 2
i=0
j=1
A:
![Page 47: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/47.jpg)
Joseph Lindo
Diagram
Quick Sort
A:
j=1
4 8 6 3 5 1 7 2
i=0
8
![Page 48: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/48.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 8 6 3 5 1 7 26
i=0
j=2
![Page 49: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/49.jpg)
Joseph Lindo
Diagram
Quick Sort
4 8 6 3 5 1 7 2
i=0
383
j=3
i=1
A:
![Page 50: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/50.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 3 6 8 5 1 7 2
i=1
5
j=4
![Page 51: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/51.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 3 6 8 5 1 7 2
i=1
1
j=5
![Page 52: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/52.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 3 6 8 5 1 7 2
i=2
1 6
j=5
![Page 53: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/53.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 3 8 5 7 2
i=2
1 6 7
j=6
![Page 54: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/54.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 3 8 5 7 2
i=2
1 6 22 8
i=3
![Page 55: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/55.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 3 2 6 7 8
i=3
1 5
![Page 56: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/56.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 4 1 6 7 8
i=3
2 542 3
![Page 57: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/57.jpg)
Joseph Lindo
Diagram
Quick Sort
A: 3 6 7 81 542
x < 4 4 ≤ x
pivot incorrect position
![Page 58: Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras](https://reader038.vdocuments.net/reader038/viewer/2022110208/56649d975503460f94a81bdc/html5/thumbnails/58.jpg)
Joseph Lindo
Code
Quick Sort
void quickSort(Object array[], int leftIdx,int rightIdx){
int pivotIdx; /* Termination condition! */ if (rightIdx > leftIdx) { pivotIdx = partition(array,
leftIdx, ightIdx); quickSort(array, leftIdx,
pivotIdx-1); quickSort(array, pivotIdx+1,
rightIdx); } }