0% found this document useful (0 votes)
5 views

ch04 Sorting Methods

data structure Sorting Methods

Uploaded by

thomaselbitar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ch04 Sorting Methods

data structure Sorting Methods

Uploaded by

thomaselbitar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction

We will see in this chapter two categories of sorting:


Quadratic algorithm
linearithmic algorithm

The well-known quadratic algorithms are:


Bubble Sort
Selection Sort
Insertion Sort

These algorithms are known as the quadratic algorithms because their Big
O time complexity is O(n2).

Well-known linearithmic sorting algorithm (time complexity is nlogn):


Merge Sort
Quick Sort
Bubble Sort
Principle (1/3):
Compare two adjacent values and swap their position if they
are not in the intended order.

If a first number x is greater than a second number y, and we


want to sort the array in ascending order.

Then, x and y are badly placed and they must be swapped.

However, if x is smaller than y, so we do nothing.


Bubble Sort
Principle (2/3):
So it's iterative.
And we go through the array in this way until we have made
n-1 passages (n representing the number of values to sort).

Principle (3/3):
In the first iteration, we place the largest element at the end of
the array.
For the next iteration, we are therefore no longer obliged to
make a comparison with the last element.
So at each iteration, the number of values to compare
decreases by 1.
Bubble Sort
Illustration of the principle: Consider the following elements: 6
0 3 5 1 4 2, We want to sort these values in ascending order.

First passage:

6035142 // we compare 0 and 6: we reverse


0635142 // we compare 6 and 3: we reverse
0365142 // we compare 6 and 5: we reverse
0356142 // we compare 6 and 1 : we reverse
0351642 // we compare 6 and 4 : we reverse
0351462 // we compare 6 and 2 : we reverse
0351426 // we have finished our first passage
Bubble Sort
Second passage: repeat the same steps but omitting the
last cell.

0 3 5 1 4 2 6 // we compare 0 and 3 : we do nothing


0 3 5 1 4 2 6 // we compare 3 and 5 : we do nothing
0 3 5 1 4 2 6 // we compare 5 and 1 : we reverse
0 3 1 5 4 2 6 // we compare 5 and 4 : we reverse
0 3 1 4 5 2 6 // we compare 5 and 2 : we reverse
0 3 1 4 2 5 6 // we have finished our second passage
Bubble Sort
Third passage:

0 3 1 4 2 5 6 // we compare 0 and 3 : we do nothing


0 3 1 4 2 5 6 // we compare 3 and 1 : we reverse
0 1 3 4 2 5 6 // we compare 3 and 4 : we do nothing
0 1 3 4 2 5 6 // we compare 4 and 2 : we reverse
0 1 3 2 4 5 6 // we have finished our third passage
Bubble Sort
Fourth passage:

0 1 3 2 4 5 6 // we compare 0 and 1 : we do nothing


0 1 3 2 4 5 6 // we compare 1 and 3 : we do nothing
0 1 3 2 4 5 6 // we compare 3 and 2 : we reverse
0 1 2 3 4 5 6 // we have finished our fourth passage
Bubble Sort
Fifth passage:

0 1 2 3 4 5 6 // we compare 0 and 1 : we do nothing


0 1 2 3 4 5 6 // we compare 1 and 2 : we do nothing
0 1 2 3 4 5 6 // we have finished our passage

The algorithm stops -> there was no more exchange


during the last passage
Bubble Sort
int tab[8] = {15, 10, 23, 2, 8, 9, 14, 16};
for (int i=0 ; i<N ; i++)
{
int j=0;
for (j=0 ; j<(N-i-1) ; j++)
{
if (tab[j]>=tab[j+1]) {
int temp = tab[j];
tab[j] = tab[j+1];
tab[j+1] = temp;

}
}
}
Selection Sort
The principle of this algorithm is to:

Find the largest element, place it at the end of the array, start over
with the second largest, place it before the last position and so on
until you have gone through the entire array.

Consider the following array of integers:


6281537940
The largest element is in the 7th position
6281537940
We exchange the largest element (in 7th position) with the
last:
6281537049
Selection Sort
The last element in the array is now necessarily the
largest.

We therefore continue by considering the same array,


while ignoring its last element:
6241537089

And so on, each time ignoring the items already


sorted.
Selection Sort
void selection_sort(int tab[ ],int size)
{
int i,j,imax,temp;
for(j=size-1;j>=0;j--){
imax=0;
for(i=1;i<=j;i++){
if(tab[imax]<tab[i]){
imax=i;
}
}
temp=tab[j];
tab[j]=tab[imax];
tab[imax]=temp;
}
}
Insertion Sort
Insertion sort is a simple sorting algorithm that works
similar to the way you sort playing cards in your hands.

It is the same idea on how you pick your cards from the
table, and you then place them in their position.

Principle:
Compare the second element with the first one. If it is greater,
then place it after the first element.
Take the third element and compare it with the elements on
the left of it. Placed it just behind the element smaller than it.
Insertion Sort: Example
Initial array:

Step 1:
Insertion Sort: Example
Step 2: Step 3:
Insertion Sort: Example
Step 4:
Insertion Sort: Code
Void insertSort(int a[ ], int size){
int i,j, key;
for( i=0;i<n;i++){
key=a[i];
for(j=i;j>0 && a[j-1]>key;j--){
a[ j ]=a[j-1]; // shifting the position to the right
}
a[ j ]=key;
}
}
QuickSort
QuickSort is a Divide and Conquer algorithm. It picks an
element as pivot and partitions the given array around the
picked pivot.

There are many different versions of quickSort that pick


pivot in different ways:
Always pick first element as pivot.
Always pick last element as pivot (implemented below)
Pick a random element as pivot.
Pick median as pivot.
QuickSort
The key process in quickSort is partition().

Target of partitions is, given an array and an element x of


array as pivot, put x at its correct position in sorted array
and put all smaller elements before x, and put all greater
elements after x.
QuickSort
arr[] = {10, 80, 30, 90, 40, 50, 70}
Indexes: 0 1 2 3 4 5 6

low = 0, high = 6, pivot = arr[high] = 70


Initialize index of smaller element, i = -1

Traverse elements from j = low to high-1


j = 0 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i = 0 arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1 : Since arr[j] > pivot, do nothing // No change in i and arr[]

j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i = 1 arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

j = 3 : Since arr[j] > pivot, do nothing // No change in i and arr[]

j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i = 2 arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped

j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
i = 3 arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
QuickSort
We come out of loop because j is now equal to high-1.
Finally we place pivot at correct position by swapping arr[i+1]
and arr[high] (or pivot)
arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped Now 70
is at its correct place.
All elements smaller than 70 are before it and all elements
greater than 70 are after it.
Partition Code:
QuickSort
The main function that implements QuickSort:
MergeSort
Like quicksort, merge sort is a divide and conquer
algorithm.

It divides input array in two halves, calls itself for the two
halves and then merges the two sorted halves.

The merge() function is used for merging two halves.

The merge (arr, l,m,r) is key process that assumes that


arr arr
two sorted sub-arrays into one.
MergeSort
The implementation process is done as follows:
if r > l
Find the middle point to divide the array into two halves:
middle: m= (l+r) / 2

Call mergeSort for first half: mergeSort (arr, l, m)

Call mergeSort for second half: mergeSort (arr, m+1, r)

Merge the two halves sorted in step 2 and 3: merge (arr, l, m, r)


MergeSort
The following diagram shows the complete merge sort process for
an example array {38, 27, 43, 3, 9, 82, 10}.

If we take a closer look at


the diagram, we can see
that the array is recursively
divided in two halves till
the size becomes 1.

Once the size becomes 1, the


merge processes comes into
action and starts merging arrays
back till the complete array is
merged.
MergeSort
4 0 6 1 5 2 3

4 0 6 1 5 2 3

4 0 6 1 5 2

40 04 61 16 52 25 3

Mergesort(0,6)
0 1 4 6 2 3 5
Merge(0,3,6)
Mergesort(4,6)
Mergesort(0,3)
Merge(4,5,6)
Mergesort(6,6)
Mergesort(4,5)
Mergesort(0,1)
Mergesort(2,3)
Merge(0,1,3)
0 1 2 3 4 5 6 Mergesort(5,5)
Merge(4,4,5)
Merge(0,0,1)
Mergesort(1,1)
Mergesort(0,0)
Mergesort(3,3)
Mergesort(2,2)
Merge(2,2,3)
Mergesort(4,4)
MergeSort Algorithm

You might also like