0% found this document useful (0 votes)
4 views87 pages

Lec 3 Algorithms

The document discusses various sorting algorithms including selection sort, insertion sort, and bubble sort, explaining their mechanisms and providing examples. It also introduces merge sort as a divide-and-conquer algorithm that breaks down an array into smaller parts for sorting. The document emphasizes the efficiency of insertion sort for smaller lists compared to bubble and selection sorts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views87 pages

Lec 3 Algorithms

The document discusses various sorting algorithms including selection sort, insertion sort, and bubble sort, explaining their mechanisms and providing examples. It also introduces merge sort as a divide-and-conquer algorithm that breaks down an array into smaller parts for sorting. The document emphasizes the efficiency of insertion sort for smaller lists compared to bubble and selection sorts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 87

Data Structure and Algorithms

Sorting Algorithms

Computer Science
Sorting
• Sorting is a process in which records are
arranged in ascending or descending
order
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6

5 12 35 42 77 101

Computer Science
Computer Science
Types of sorting
• Selection sort
• Insertion sort
• Bubble sort
• Merge sort
• Quick sort
• Radix sort
• Shell sort
• Bucket sort
Computer Science
Selection Sort

Computer Science
Selection Sort
• Selection sort is a sorting algorithm which works as
follows:
– Find the minimum value in the list
– Swap it with the value in the first position
– Repeat the steps above for remainder of the list
(starting at the second position)

Computer Science
Example: Selection Sort
• 26 33 43 100 46 88 52 17 53 77
• 17 | 33 43 100 46 88 52 26 53 77
• 17 26 | 43 100 46 88 52 33 53 77
• 17 26 33 | 100 46 88 52 43 53 77
• 17 26 33 43 | 46 88 52 100 53 77
• 17 26 33 43 46 | 88 52 100 53 77
• 17 26 33 43 46 52 | 88 100 53 77
• 17 26 33 43 46 52 53 | 100 88 77
• 17 26 33 43 46 52 53 77 | 88 100
• 17 26 33 43 46 52 53 77 88 | 100

Computer Science
Selection Sort
{ void selection Sort(int numbers[], int array_size)
;int i, j, T, min, count
{ for (i = 0; i < array_size; i++)
;min = i
{ for (j = i + 1; j < array_size; j++)
{ if (numbers[j] < numbers[min])
} ;min = j
}
;T = numbers[min]
;numbers[min] = numbers[i]
;numbers[i] = T
}
}

Computer Science
Insertion Sort

Computer Science
Insertion Sort
• In insertion sort, each successive element in the
array to be sorted is inserted into its proper place
with respect to the other, already sorted elements.

• We divide our array in a sorted and an unsorted


array

• Initially the sorted portion contains only one


element: the first element in the array.

• We take the second element in the array, and put


it into its correct place
Computer Science
Insertion Sort
• That is, array[0] and array[1] are in order with
respect to each other.
• Then the value in array[2] is put into its proper
place, so array [0]…. array[2] is sorted and so
on.

36 36 24 10 6 6
24 24 36 24 10 10
10 10 10 36 24 12
6 6 6 6 36 24
12 12 12 12 12 36

Computer Science
Insertion Sort
• Our strategy is to search for insertion point from
the beginning of the array and shift the element
down to make room for new element.

• We compare the item at array[current] to one


before it, and swap if it is less.

• We then compare array[current-1] to one before


it and swap if necessary.

Computer Science
Example: Insertion Sort
• 99 | 55 4 66 28 31 36 52 38 72
• 55 99 | 4 66 28 31 36 52 38 72
• 4 55 99 | 66 28 31 36 52 38 72
• 4 55 66 99 | 28 31 36 52 38 72
• 4 28 55 66 99 | 31 36 52 38 72
• 4 28 31 55 66 99 | 36 52 38 72
• 4 28 31 36 55 66 99 | 52 38 72
• 4 28 31 36 52 55 66 99 | 38 72
• 4 28 31 36 38 52 55 66 99 | 72
• 4 28 31 36 38 52 55 66 72 99 |

Computer Science
Insertion Sort Algorithm
void insertionSort(int array[], int length)
{
int i, j, value;
for(i = 1; i < length; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[ j ] > value; j--)
{
a[j + 1] = a[ j ];
}
a[j + 1] = value;
}
}

Computer Science
Bubble Sort

Computer Science
Bubble Sort
• Bubble sort is similar to selection
sort in the sense that it repeatedly
finds the largest/smallest value in the
unprocessed portion of the array and
puts it back.
• However, finding the largest value
is not done by selection this time.
• We "bubble" up the largest value
instead.

Computer Science
Bubble Sort
• Compares adjacent items and exchanges them
if they are out of order.
• Comprises of several passes.
• In one pass, the largest value has been
“bubbled” to its proper position.
• In second pass, the last value does not need to
be compared.

Computer Science
Bubble Sort
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

77 42 35 12 101 5

Computer Science
Bubble Sort
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 Swap42
77 77 35 12 101 5

Computer Science
Bubble Sort
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 Swap 35
77 77 12 101 5

Computer Science
Bubble Sort
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 Swap 12
77 77 101 5

Computer Science
Bubble Sort
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap

Computer Science
Bubble Sort
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 Swap 101
101 5

Computer Science
Bubble Sort
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed

Computer Science
Items of Interest
• Notice that only the largest value is correctly
placed
• All other values are still out of order
• So we need to repeat this process

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed

Computer Science
“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
N-1

12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101

1 2 3 4 5 6
5 12 35 42 77 101

Computer Science
Reducing the Number of
Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101

Computer Science
?Already Sorted Collections
• What if the collection was already sorted?
• What if only a few elements were out of place and
after a couple of “bubble ups,” the collection was
sorted?

• We want to be able to detect this and “stop early”!

1 2 3 4 5 6
5 12 35 42 77 101

Computer Science
”Using a Boolean “Flag
• We can use a boolean variable to determine if
any swapping occurred during the “bubble up.”

• If no swapping occurred, then we know that the


collection is already sorted!

• This boolean “flag” needs to be reset after each


“bubble up.”

Computer Science
Bubble Sort Algorithm
void bubbleSort (int S[ ], int length) {
bool isSorted = false;
while(!isSorted)
{
isSorted = true;
for(int i = 0; i<length; i++)
{
if(S[i] > S[i+1])
{
int temp = S[i];
S[i] = S[i+1];
S[i+1] = temp;
isSorted = false;
}
}
length--;
}
}

Computer Science
Insertion Sort

Selection Sort

Bubble Sort
Computer Science
Summary
The insertion sort is a good middle-of-the-road
choice for sorting lists of a few thousand items
.or less
The insertion sort is over twice as fast as the
bubble sort and almost 40% faster than the
selection sort. The insertion sort shouldn't be
used for sorting lists larger than a couple
thousand items or repetitive sorting of lists larger
.than a couple hundred items

Computer Science
Mergesort

Computer Science
Divide and Conquer
• Divide and Conquer cuts the problem in half
each time, but uses the result of both halves:
– cut the problem in half until the problem is
trivial
– solve for both halves
– combine the solutions

Computer Science
Merge Sort

Computer Science
Mergesort
• A divide-and-conquer algorithm:
• Divide the unsorted array into 2 halves until the
sub-arrays only contain one element
• Merge the sub-problem solutions together:
– Compare the sub-array’s first elements
– Remove the smallest element and put it into
the result array
– Continue the process until all elements have
been put into the result array

37 23 6 89 15 12 2 19

Computer Science
98 23 45 14 6 67 33 42

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge

Computer Science
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Computer Science
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98

Computer Science
Algorithm
Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves.

Merge(Passed two arrays)


Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output array)

Computer Science
Merge Sort
• We don’t really pass in two arrays!

• We pass in one array with indicator variables which tell


us where one set of data starts and finishes and where
the other set of data starts and finishes.

s1 f1 s2 f2
Computer Science
Merge Sort
;void merge(int*,int*,int,int,int)
{ void mergesort(int *a, int*b, int low, int high)
;int pivot
{ if(low<high)
;pivot=(low+high)/2
;mergesort(a,b,low,pivot)
;mergesort(a,b,pivot+1,high)
;merge(a,b,low,pivot,high)
}
}
{ )(int main
;int a[] = {12,10,43,23,78,45,56,98,41,90,24}
;int num; num = sizeof(a)/sizeof(int)
;int b[num]
;mergesort(a,b,0,num-1)
} ;for(int i=0; i<num; i++) cout<<a[i]<<" "; cout<<endl

Computer Science
void merge(int *a, int *b, int low, int pivot,
{ int high)
;int h,i,j,k; h=low; i=low; j=pivot+1
{ while((h<=pivot)&&(j<=high))
if(a[h]<=a[j]) { b[i]=a[h]; h++; }
else { b[i]=a[j]; j++; }
} ;++i
{ if(h>pivot)
{ for(k=j; k<=high; k++)
} } ;++b[i]=a[k]; i
{ else
{ for(k=h; k<=pivot; k++)
} } ;++b[i]=a[k]; i
} ;for(k=low; k<=high; k++) a[k]=b[k]

Computer Science
Quicksort / partition-exchange sort
.Quick sort is a divide and conquer algorithm
Quick sort first divides a large list into two smaller sub-lists: the low elements
.and the high elements. Quick sort can then recursively sort the sub-lists

:The steps are


.Pick an element, called a pivot, from the list .1
Reorder the list so that all elements with values less than the pivot come .2
before the pivot, while all elements with values greater than the pivot come
after it (equal values can go either way). After this partitioning, the pivot is
.in its final position. This is called the partition operation
Recursively apply the above steps to the sub-list of elements with smaller .3
.values and separately the sub-list of elements with greater values
The base case of the recursion are lists of size zero or one, which never
.need to be sorted

Computer Science
Quick Sort

Computer Science
Quick Sort
;void quickSort(int a[], int first, int last)
;int pivot(int a[], int first, int last)
;void swap(int& a, int& b)
)(Void main
{
;int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 }
;int N = sizeof(test)/sizeof(int)
;quickSort(test, 0, N-1)
}
{ void swap(int& a, int& b)
;int temp = a
;a = b
;b = temp
}
Computer Science
{ void quickSort( int a[], int first, int last)
;int pivotElement
{ if(first < last)
;pivotElement = pivot(a, first, last)
;quickSort(a, first, pivotElement-1)
} ;quickSort(a, pivotElement+1, last)
}
{int pivot(int a[], int first, int last)
;int p = first
;int pivotElement = a[first]
{for(int i = first+1 ; i <= last ; i++)
{if(a[i] <= pivotElement)
;++p
;swap(a[i], a[p])
}
}
;swap(a[p], a[first])
;return p
Computer Science
{int pivot(int a[], int first, int last)
;int p = first
;int pivotElement = a[first]
{for(int i = first+1 ; i <= last ; i++)
{if(a[i] <= pivotElement)
;++p
;swap(a[i], a[p])
}
}
;swap(a[p], a[first])
;return p
}

Computer Science
Useful Links

http://www.csanimated.com/animation.php?t=Quick
sort
http://www.hakansozer.com/category/c/
og/2012/11/27/computer
sort/
http://www.sorting-algorithms.com/shell-sort

Computer Science

You might also like