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

Chapter 3 Sorting Techniques

Uploaded by

Adil Sajjad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Chapter 3 Sorting Techniques

Uploaded by

Adil Sajjad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

SORTING

TECHNIQUES
Sorting

• Refers to the operation of arranging the data in some


given order, such as increasing or decreasing.
• For Example let A be a list of n numbers then:
A[1]<A[2]<A[3]<…………..A[n]
Sorting Techniques
Some of the sorting techniques used to arrange data are
as follows :
• Bubble sort
• Selection sort
• Insertion sort
• Merge sort
• Quick sort
Bubble Sort

• The general idea of the bubble sort is that each element is


compared to all the elements in the data.
• The bubble sort algorithm requires N-1 passes to sort N
items of data.
• The bubble sort is notoriously slow, but it is conceptually
the simplest of the sorting algorithms.
1. The result of the first pass is that the largest item is in the last
location of the array.
2. The result of the second pass is that the second largest item is
in the second last location of the array.
3. etc.
4. After N passes, the entire array is sorted.
Bubble Sort

The operation in each pass is as follows:


1. First, the values in the first two locations are compared. If
necessary the values are exchanged, so that the largest one is
last.
2. Then, the values in the second and third locations are
compared. If necessary the values are exchanged, so that
again the largest one is last.
3. This process repeats to the end of the array.
4. In effect, the largest item bubbles its way towards the top. It
keeps on going until either it reaches the top and the pass
ends.
• If a complete pass is made without any exchanges being
made, the data must already be sorted.
Example

• Suppose the following numbers are stored in an array A:


32,51,27,85,66,23,13,57
Pass 1:
a) Compare A[1] and A[2]:Since 32<51 the list is not altered.
b) Compare A[2] and A[3]: Since 51>27 interchange 51 and 27.
32,27,51,85,66,23,13,57
c) Compare A[3] and A[4]:Since 51<85 the list is not altered.
d) Compare A[4] and A[5]: Since 85>66 interchange 85 and 66.
32,27,51,66,85,23,13,57
e) Compare A[5] and A[6]: Since 85>23 interchange 85 and 23.
32,27,51,66,23,85,13,57
f) Compare A[6] and A[7]: Since 85>13 interchange 85 and 13.
g) 32,27,51,66,23,13,85,57
h) Compare A[7] and A[8]: Since 85>57 interchange 85 and 57.
i) 32,27,51,66,23,13,57,85
Bubble Sort Algorithm

Bubble sort (A)


{
for (pass=1;pass<n;pass++)
for (c=1;c<=(n-pass);c++)
if (A[c]>A[c+1])
{
swap (A[c], A[c+1])
}

}
Complexity of Bubble Sort

• Worst Case:
The worst case occurs when we have to search through the entire
array of data and the item does not appear in data. In this case the
algorithm requires f(n) = n-1 comparisons.
• Average Case:
The average number of comparisons required to find the location
item is approximately equal to half number of elements in the array
that is :
f(n) = (n-1)/2 comparisons.
Complexity of Bubble Sort
• Time consuming operations
• compares, swaps.
• Compares
• a for loop embedded inside a for loop
• (n-1)+(n-2)+(n-3) …+1 , or O(n2)
• Swaps
• inside a conditional -> #swaps data dependent !!
• Best Case 0, or O(1)
• Worst Case (n-1)+(n-2)+(n-3) …+1 , or O(n2)
• Average Case O(n2)
• Space
• size of the array
• an in-place algorithm
Selection Sort
• The general idea of the selection sort is that for each slot,
find the element that belongs there.
• The selection sort improves on the bubble sort by
reducing the number of swaps.
• The number of comparisons remains the same that is to
sort N items you make N-1 passes through them.
Selection Sort
• The operation in each pass is as follows:
1. First find the smallest element in the list and swap it with the
element in the first position.
2. On the second pass you scan through just the first N-1 entries.
Then find the second smallest element in the list and swap it in
the element in the second position.
3. This process is repeated, with one item being placed in its
correct location each time.
4. After N-1 passes, the entire collection of data is sorted.
Example

• Suppose the following numbers are stored in an array A:

77,33,44,11,88,22,66,55

Pass 1: 77,33,44,11,88,22,66,55
Pass 2: 11,33,44,77,88,22,66,55
Pass 3: 11,22,44,77,88,33,66,55
Pass 4: 11,22,33,77,88,44,66,55
Pass 5: 11,22,33,44,88,77,66,55
Pass 6: 11,22,33,44,55,77,66,88
Pass 7: 11,22,33,44,55,66,77,88
Selection Sort Algorithm
Selection sort (A)
{
for (i=0;i<n-1;i++)
{
min=i;
for (j=i+1;j<n;j++)
{
if (A[j]<A[min])
min=j;
}
swap (A[min],A[i])
}
}
Complexity of Selection Sort

Best Case Worst Case

O(n2) O(n2)
15

Insertion Sort
• Idea: like sorting a hand of playing cards
• Start with an empty left hand and the cards facing down on the
table.
• Remove one card at a time from the table, and insert it into the
correct position in the left hand
• compare it with each of the cards already in the hand, from right to left
• The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the table
16

Insertion Sort

To insert 12, we need to


make room for it by moving
first 36 and then 24.
6 10 24 36

12
17

Insertion Sort

6 10 24 36

12
18

Insertion Sort

6 10 24 3
6

12
19

Insertion Sort
input array

5 2 4 6 1
3
at each iteration, the array is divided in two sub-arrays:

left sub-array right sub-array

sorted unsorted
20

Insertion Sort
Insertion Sort
• The operation in each pass is as follows:
1. A[1 ] by itself is sorted.
2. A[2] is inserted either before A[1] or after A[1] so that A[1] and
A[2] are sorted.
3. A[N] is inserted into its proper place in A[1],A[2]…A[N-1] so
that A[1],A[2]…A[N] is sorted.
Example
• Suppose the following numbers are stored in an array A:
77,33,44,11,88,22,66,55
Pass 1:77,33,44,11,88,22,66,55
Pass 2:77,33,44,11,88,22,66,55
Pass 3:33,77,44,11,88,22,66,55
Pass 4:33,44,77,11,88,22,66,55
Pass 5:11,33,44,77,88,22,66,55
Pass 6:11,22,33,44,77,88,66,55
Pass 7:11,22,33,44,66,77,88,55
Pass 8:11,22,33,44,55,66,77,88
Insertion Sort Algorithm
Insertion sort (A)
{
for (i=1;i<n-1;i++)
{
key=A[i]
j=i-1;
while (j>=0 && A[j]>key)
{
A[j+1]=A[j];
j--;
}
A[j+1]=key;
}
}
Complexity of Insertion Sort

Best Case Worst Case

O(1) O(n2)
Divide and Conquer
• Divide and Conquer
• Very important strategy in computer science
• Divide problem into smaller parts
• Independently solve the parts
• Combine these solutions to get overall solution
• Idea 1:Divide array into two halves, recursively sort left
and right halves, then merge two halves known as Merge
sort
• Idea 2 : Partition array into small items and large items,
then recursively sort the two sets known as Quicksort
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M O auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M O R auxiliary array
Merging
•Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S auxiliary array
Mergesort
•Mergesort (divide-and-conquer)
• Divide array into two halves.

A L G O R I T H M S

A L G O R I T H M S divide
Mergesort
•Mergesort (divide-and-conquer)
• Divide array into two halves.
• Recursively sort each half.

A L G O R I T H M S

A L G O R I T H M S divide

A G L O R H I M S T sort
Mergesort
•Mergesort (divide-and-conquer)
• Divide array into two halves.
• Recursively sort each half.
• Merge two halves to make sorted whole.

A L G O R I T H M S

A L G O R I T H M S divide

A G L O R H I M S T sort

A G H I L M O R S T merge
Merge Sort Algorithm
Mergesort(A)
{
n=length(A)
If(n<2)
return
mid=n/2
left=array of size(mid)
right=array of size(n-mid)
for(i=0 to mid-1)
left [i]=A[i]
for(i=mid to n-1)
right[i-mid]=A[i]
MergeSort(left)
MergeSort(right)
Merge(left,right,A)
}
Quick Sort
• Quicksort is a divide-and-conquer sorting algorithm in
which division is dynamically carried out.
• Divide: Rearrange the elements and split the array into
two subarrays and an element in between such that so
that each element in the left subarray is less than or equal
the middle element and each element in the right subarray
is greater than the middle element.

• Conquer: Recursively sort the two subarrays


Select pivot

There are a number of ways to pick the pivot element. In this


example, we will use the last element in the array

7 2 1 6 8 5 3 4

Pivot
Partitioning Array

2 1 3 4 8 5 7 6
Quick Sort
QuickSort(A.start,end)
{
if(start<end)
{
Pindex=Partition(A,start,end)
QuickSort(A,start,pindex-1)
QuickSort(A,pindex+1,end)
}
}
Partitioning Array

Given a pivot, partition the elements of the array


such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data


array.

Partitioning loops through, swapping elements


below/above pivot.
Quick sort
Partition(A,start,end)
{
Pivot=a[end]
Pindex=start
for(i=start to end-1)
{
If(A[i]<=pivot)
{
swap(A[i],A[index])
Pindex=pindex+1}
}
Swap(A[Pindex],A[end]
}

You might also like