Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras Joseph Lindo Sorting Sorting Definition Reason An operation that segregates items into groups according to specified criterion A={3162134590} Algorithms A={0112334569} Activity.
Download ReportTranscript Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras Joseph Lindo Sorting Sorting Definition Reason An operation that segregates items into groups according to specified criterion A={3162134590} Algorithms A={0112334569} Activity.
Slide 1
Joseph Lindo
Sorting
Algorithms
Sir Joseph Lindo
University of the Cordilleras
Slide 2
Joseph Lindo
Sorting
Sorting
Definition
Reason
An operation that
segregates items into groups
according to specified criterion
A={3162134590}
Algorithms
A={0112334569}
Activity
Slide 3
Joseph Lindo
Sorting
Definition
Why sorting?
Examples:
Reason
Reason
Algorithms
Activity
Sorting Books in Library (Dewey
system)
Sorting Individuals by Height (Feet
and Inches)
Slide 4
Joseph Lindo
Sorting
Definition
Why sorting?
Examples:
Reason
Reason
Algorithms
Activity
Sorting Movies in Blockbuster
(Alphabetical)
Sorting Numbers (Sequential)
Slide 5
Joseph Lindo
Sorting
Definition
Reason
Reason
Algorithms
Activity
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
Slide 6
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Algorithms
Activity
Sorting
Algorithms
Slide 7
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Algorithms
Activity
Sorting
Algorithms
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Slide 8
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Activity
Activity
Group
Case Study
Group maximum of five members
Short bond paper
Create the flowchart of:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Slide 9
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Activity
Activity
Group
Case Study
Group maximum of five members
Yellow paper
Construct your own “Sorting
Algorithm”
Provide example
Provide the Code
Slide 10
Joseph Lindo
Sorting
Algorithms
--end-Sir Joseph Lindo
University of the Cordilleras
Slide 11
Sorting Algorithms
Bubble
Bubble
Selection
Insertion
Merge
Quick
Joseph Lindo
Bubble Sort
The simple idea is keep
passing through the list,
swapping the next element that
is out of order, until the list is
sorted.
Slide 12
Sorting Algorithms
Bubble
Selection
Selection
Insertion
Merge
Quick
Joseph Lindo
Selection Sort
Works by selecting
elements in specific order, and
putting them in proper position in
the final list
It is an in-place sorting algorithm
Slide 13
Sorting Algorithms
Bubble
Selection
Insertion
Insertion
Merge
Quick
Joseph Lindo
Insertion Sort
It is a comparison based
sort, which sorts the array one
entry at a time.
Slide 14
Sorting Algorithms
Bubble
Selection
Insertion
Merge
Merge
Quick
Joseph Lindo
Merge Sort
Original problem is split into
subproblems
Solutions to subproblems lead to
the solution of the main
problem
Slide 15
Sorting Algorithms
Bubble
Selection
Insertion
Merge
Quick
Quick
Joseph Lindo
Quick Sort
Invented by C.A.R. Hoare
Also based on the divide-andconquer paradigm
Slide 16
Stable Algorithm
Joseph Lindo
Characteristics
A stable sort keeps equal elements in the same
order
This may matter when you are sorting data
according to some characteristic
jo
Example: sorting students by test scores
Slide 17
Stable Algorithm
Characteristics
Ann
98
Ann
98
Bob
90
Joe
98
Dan
75
Bob
90
Joe
98
Sam
90
Pat
86
Pat
86
Sam
90
Zöe
86
Zöe
86
Dan
75
original array
jo
stably sorted
Joseph Lindo
Slide 18
Unstable Algorithm
Joseph Lindo
Characteristics
An unstable sort may or may not keep equal
elements in the same order
Stability is usually not important, but sometimes
it is important
Slide 19
Unstable Algorithm
Characteristics
Ann
98
Joe
98
Bob
90
Ann
98
Dan
75
Bob
90
Joe
98
Sam
90
Pat
86
Zöe
86
Sam
90
Pat
86
Zöe
86
Dan
75
original array
unstably
sorted
Joseph Lindo
Slide 20
Bubble Sort
Joseph Lindo
Characteristics
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
Slide 21
Joseph Lindo
Bubble Sort
Diagram
7 2 8 5 4
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 7 8 5 4
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 7 8 5 4
2 5 7 4 8
2 4 5 7 8
(done)
2 7 5 8 4
2 5 4 7 8
2 7 5 4 8
Slide 22
Bubble Sort
Joseph Lindo
Code
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;
}
}
}
}
Slide 23
Selection Sort
Joseph Lindo
Characteristics
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
Slide 24
Selection Sort
Diagram
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
Joseph Lindo
Slide 25
Selection Sort
Joseph Lindo
Code
public static void selectionSort(int[] a){
int outer, inner, min;
for(outer=0;outer min = outer;
for(inner=outer+1;inner if (a[inner] < a[min]){
min = inner;
}
}
int temp = a[outer];
a[outer] = a[min];
a[min] = temp;
}
}
Slide 26
Insertion Sort
Joseph Lindo
Characteristics
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
Slide 27
Insertion Sort
Joseph Lindo
Diagram
sorted
next to be inserted
3 4 7 121414202133381055 9 232816
less
than 10
3 4 7 10 12 14 14 20 21 33 38 55 9 232816
temp
10
sorted
27
Slide 28
Insertion Sort
Joseph Lindo
Code
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;
}
}
}
Slide 29
Merge Sort
Joseph Lindo
Characteristics
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
Slide 30
Joseph Lindo
Merge Sort
Diagram
Divide into A
two halves
FirstPart
SecondPart
SORT
SecondPart
FirstPart
Merge
A is sorted!
Slide 31
Joseph Lindo
Merge Sort
Diagram
A:
2
5
3
5
7 28
8 30
1
15
4
6
5 14
6
10
R:
L:
3
5
15 28
6
Temporary Arrays
10 14 22
Slide 32
Joseph Lindo
Merge Sort
Diagram
A:
3
1
5
15 28 30
6
10 14
k=0
R:
L:
3
2
i=0
15
3 28
7 30
8
6
1
j=0
10
4 14
5 22
6
Slide 33
Joseph Lindo
Merge Sort
Diagram
A:
1
2
5
15 28 30
6
10 14
k=1
R:
L:
3
2
i=0
5
3
15
7 28
8
6
1
10
4 14
5 22
6
j=1
Slide 34
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3 28 30
15
6
10 14
k=2
R:
L:
2
3
i=1
7
8
6
1
10
4 14
5 22
6
j=1
Slide 35
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
6
10 14
k=3
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
j=1
Slide 36
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
6
10 14
k=4
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
j=2
Slide 37
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
6
10 14
k=5
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
j=3
Slide 38
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
7
6
14
k=6
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
Slide 39
Joseph Lindo
Merge Sort
Diagram
-
A:
1
2
3
4
5
6
7
8
14
k=7
R:
L:
3
2
5
3
15
7 28
8
i=3
6
1
10
4 14
5 22
6
Slide 40
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
6
7
8
k=8
R:
L:
3
2
5
3
15
7 28
8
6
1
i=4
10
4 14
5 22
6
Slide 41
Merge Sort
Joseph Lindo
Code
void mergeSort(Object array[], int
startIdx,int endIdx){
if (array.length != 1) {
mergeSort(leftArr,startIdx, midIdx);
mergeSort(rightArr, midIdx+1,endIdx);
combine(leftArr, rightArr);
}
}
Slide 42
Quick Sort
Joseph Lindo
Characteristics
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
Slide 43
Joseph Lindo
Quick Sort
Diagram
A:
pivot
p
Partition
SecondPart
FirstPart
x
p
Sorted
FirstPart
xSorted
p≤x
Sorted
SecondPart
p
p≤x
Slide 44
Joseph Lindo
Quick Sort
Diagram
A: p
A: p
A: p
x
x
p≤x
p
p≤x
Slide 45
Joseph Lindo
Quick Sort
Diagram
A: 4
8
6
3
5
1
7
2
Slide 46
Joseph Lindo
Quick Sort
Diagram
i=0
A: 4
8
j=1
6
3
5
1
7
2
Slide 47
Joseph Lindo
Quick Sort
Diagram
i=0
A: 4
8
j=1
6
3
5
1
7
2
Slide 48
Joseph Lindo
Quick Sort
Diagram
i=0
A: 4
8
6
j=2
3
5
1
7
2
Slide 49
Joseph Lindo
Quick Sort
Diagram
i=0i=1
A: 4
8
3
6
3
8
j=3
5
1
7
2
Slide 50
Joseph Lindo
Quick Sort
Diagram
i=1
A: 4
3
6
8
5
j=4
1
7
2
Slide 51
Joseph Lindo
Quick Sort
Diagram
i=1
A: 4
3
6
8
5
1
j=5
7
2
Slide 52
Joseph Lindo
Quick Sort
Diagram
i=2
A: 4
3
1
6
8
5
6
1
j=5
7
2
Slide 53
Joseph Lindo
Quick Sort
Diagram
i=2
A: 4
3
1
8
5
6
7
j=6
2
Slide 54
Joseph Lindo
Quick Sort
Diagram
i=2i=3
A: 4
3
1
2
8
5
6
7
8
2
Slide 55
Joseph Lindo
Quick Sort
Diagram
i=3
A: 4
3
1
2
5
6
7
8
Slide 56
Joseph Lindo
Quick Sort
Diagram
i=3
A: 24
3
1
4
2
5
6
7
8
Slide 57
Joseph Lindo
Quick Sort
Diagram
A: 2
3
x<4
1
4
5
6
7
4≤x
pivot in
correct position
8
Slide 58
Quick Sort
Joseph Lindo
Code
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);
}
}
Joseph Lindo
Sorting
Algorithms
Sir Joseph Lindo
University of the Cordilleras
Slide 2
Joseph Lindo
Sorting
Sorting
Definition
Reason
An operation that
segregates items into groups
according to specified criterion
A={3162134590}
Algorithms
A={0112334569}
Activity
Slide 3
Joseph Lindo
Sorting
Definition
Why sorting?
Examples:
Reason
Reason
Algorithms
Activity
Sorting Books in Library (Dewey
system)
Sorting Individuals by Height (Feet
and Inches)
Slide 4
Joseph Lindo
Sorting
Definition
Why sorting?
Examples:
Reason
Reason
Algorithms
Activity
Sorting Movies in Blockbuster
(Alphabetical)
Sorting Numbers (Sequential)
Slide 5
Joseph Lindo
Sorting
Definition
Reason
Reason
Algorithms
Activity
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
Slide 6
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Algorithms
Activity
Sorting
Algorithms
Slide 7
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Algorithms
Activity
Sorting
Algorithms
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Slide 8
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Activity
Activity
Group
Case Study
Group maximum of five members
Short bond paper
Create the flowchart of:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Slide 9
Joseph Lindo
Sorting
Definition
Reason
Algorithms
Activity
Activity
Group
Case Study
Group maximum of five members
Yellow paper
Construct your own “Sorting
Algorithm”
Provide example
Provide the Code
Slide 10
Joseph Lindo
Sorting
Algorithms
--end-Sir Joseph Lindo
University of the Cordilleras
Slide 11
Sorting Algorithms
Bubble
Bubble
Selection
Insertion
Merge
Quick
Joseph Lindo
Bubble Sort
The simple idea is keep
passing through the list,
swapping the next element that
is out of order, until the list is
sorted.
Slide 12
Sorting Algorithms
Bubble
Selection
Selection
Insertion
Merge
Quick
Joseph Lindo
Selection Sort
Works by selecting
elements in specific order, and
putting them in proper position in
the final list
It is an in-place sorting algorithm
Slide 13
Sorting Algorithms
Bubble
Selection
Insertion
Insertion
Merge
Quick
Joseph Lindo
Insertion Sort
It is a comparison based
sort, which sorts the array one
entry at a time.
Slide 14
Sorting Algorithms
Bubble
Selection
Insertion
Merge
Merge
Quick
Joseph Lindo
Merge Sort
Original problem is split into
subproblems
Solutions to subproblems lead to
the solution of the main
problem
Slide 15
Sorting Algorithms
Bubble
Selection
Insertion
Merge
Quick
Quick
Joseph Lindo
Quick Sort
Invented by C.A.R. Hoare
Also based on the divide-andconquer paradigm
Slide 16
Stable Algorithm
Joseph Lindo
Characteristics
A stable sort keeps equal elements in the same
order
This may matter when you are sorting data
according to some characteristic
jo
Example: sorting students by test scores
Slide 17
Stable Algorithm
Characteristics
Ann
98
Ann
98
Bob
90
Joe
98
Dan
75
Bob
90
Joe
98
Sam
90
Pat
86
Pat
86
Sam
90
Zöe
86
Zöe
86
Dan
75
original array
jo
stably sorted
Joseph Lindo
Slide 18
Unstable Algorithm
Joseph Lindo
Characteristics
An unstable sort may or may not keep equal
elements in the same order
Stability is usually not important, but sometimes
it is important
Slide 19
Unstable Algorithm
Characteristics
Ann
98
Joe
98
Bob
90
Ann
98
Dan
75
Bob
90
Joe
98
Sam
90
Pat
86
Zöe
86
Sam
90
Pat
86
Zöe
86
Dan
75
original array
unstably
sorted
Joseph Lindo
Slide 20
Bubble Sort
Joseph Lindo
Characteristics
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
Slide 21
Joseph Lindo
Bubble Sort
Diagram
7 2 8 5 4
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 7 8 5 4
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 7 8 5 4
2 5 7 4 8
2 4 5 7 8
(done)
2 7 5 8 4
2 5 4 7 8
2 7 5 4 8
Slide 22
Bubble Sort
Joseph Lindo
Code
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;
}
}
}
}
Slide 23
Selection Sort
Joseph Lindo
Characteristics
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
Slide 24
Selection Sort
Diagram
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
Joseph Lindo
Slide 25
Selection Sort
Joseph Lindo
Code
public static void selectionSort(int[] a){
int outer, inner, min;
for(outer=0;outer
for(inner=outer+1;inner
min = inner;
}
}
int temp = a[outer];
a[outer] = a[min];
a[min] = temp;
}
}
Slide 26
Insertion Sort
Joseph Lindo
Characteristics
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
Slide 27
Insertion Sort
Joseph Lindo
Diagram
sorted
next to be inserted
3 4 7 121414202133381055 9 232816
less
than 10
3 4 7 10 12 14 14 20 21 33 38 55 9 232816
temp
10
sorted
27
Slide 28
Insertion Sort
Joseph Lindo
Code
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;
}
}
}
Slide 29
Merge Sort
Joseph Lindo
Characteristics
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
Slide 30
Joseph Lindo
Merge Sort
Diagram
Divide into A
two halves
FirstPart
SecondPart
SORT
SecondPart
FirstPart
Merge
A is sorted!
Slide 31
Joseph Lindo
Merge Sort
Diagram
A:
2
5
3
5
7 28
8 30
1
15
4
6
5 14
6
10
R:
L:
3
5
15 28
6
Temporary Arrays
10 14 22
Slide 32
Joseph Lindo
Merge Sort
Diagram
A:
3
1
5
15 28 30
6
10 14
k=0
R:
L:
3
2
i=0
15
3 28
7 30
8
6
1
j=0
10
4 14
5 22
6
Slide 33
Joseph Lindo
Merge Sort
Diagram
A:
1
2
5
15 28 30
6
10 14
k=1
R:
L:
3
2
i=0
5
3
15
7 28
8
6
1
10
4 14
5 22
6
j=1
Slide 34
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3 28 30
15
6
10 14
k=2
R:
L:
2
3
i=1
7
8
6
1
10
4 14
5 22
6
j=1
Slide 35
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
6
10 14
k=3
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
j=1
Slide 36
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
6
10 14
k=4
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
j=2
Slide 37
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
6
10 14
k=5
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
j=3
Slide 38
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
7
6
14
k=6
R:
L:
2
3
7
i=2
8
6
1
10
4 14
5 22
6
Slide 39
Joseph Lindo
Merge Sort
Diagram
-
A:
1
2
3
4
5
6
7
8
14
k=7
R:
L:
3
2
5
3
15
7 28
8
i=3
6
1
10
4 14
5 22
6
Slide 40
Joseph Lindo
Merge Sort
Diagram
A:
1
2
3
4
5
6
7
8
k=8
R:
L:
3
2
5
3
15
7 28
8
6
1
i=4
10
4 14
5 22
6
Slide 41
Merge Sort
Joseph Lindo
Code
void mergeSort(Object array[], int
startIdx,int endIdx){
if (array.length != 1) {
mergeSort(leftArr,startIdx, midIdx);
mergeSort(rightArr, midIdx+1,endIdx);
combine(leftArr, rightArr);
}
}
Slide 42
Quick Sort
Joseph Lindo
Characteristics
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
Slide 43
Joseph Lindo
Quick Sort
Diagram
A:
pivot
p
Partition
SecondPart
FirstPart
x
p
Sorted
FirstPart
xSorted
p≤x
Sorted
SecondPart
p
p≤x
Slide 44
Joseph Lindo
Quick Sort
Diagram
A: p
A: p
A: p
x
x
p≤x
p
p≤x
Slide 45
Joseph Lindo
Quick Sort
Diagram
A: 4
8
6
3
5
1
7
2
Slide 46
Joseph Lindo
Quick Sort
Diagram
i=0
A: 4
8
j=1
6
3
5
1
7
2
Slide 47
Joseph Lindo
Quick Sort
Diagram
i=0
A: 4
8
j=1
6
3
5
1
7
2
Slide 48
Joseph Lindo
Quick Sort
Diagram
i=0
A: 4
8
6
j=2
3
5
1
7
2
Slide 49
Joseph Lindo
Quick Sort
Diagram
i=0i=1
A: 4
8
3
6
3
8
j=3
5
1
7
2
Slide 50
Joseph Lindo
Quick Sort
Diagram
i=1
A: 4
3
6
8
5
j=4
1
7
2
Slide 51
Joseph Lindo
Quick Sort
Diagram
i=1
A: 4
3
6
8
5
1
j=5
7
2
Slide 52
Joseph Lindo
Quick Sort
Diagram
i=2
A: 4
3
1
6
8
5
6
1
j=5
7
2
Slide 53
Joseph Lindo
Quick Sort
Diagram
i=2
A: 4
3
1
8
5
6
7
j=6
2
Slide 54
Joseph Lindo
Quick Sort
Diagram
i=2i=3
A: 4
3
1
2
8
5
6
7
8
2
Slide 55
Joseph Lindo
Quick Sort
Diagram
i=3
A: 4
3
1
2
5
6
7
8
Slide 56
Joseph Lindo
Quick Sort
Diagram
i=3
A: 24
3
1
4
2
5
6
7
8
Slide 57
Joseph Lindo
Quick Sort
Diagram
A: 2
3
x<4
1
4
5
6
7
4≤x
pivot in
correct position
8
Slide 58
Quick Sort
Joseph Lindo
Code
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);
}
}