You are on page 1of 150

SORTING

By: Name :- Arshpreet Singh Section: - RK22H1B30 Reg No. 11107537

Sorting
The Process of rearranging the elements so that they are in ascending order or descending order is called sorting. Example: arranging of numbers, student records. very essential for searching the dictionaries, telephone directories.

Properties
The two Main properties of sorting techniques are: Stable: If the sorting algorithm preserves the relative order of any two equal elements, then the sorting algorithm is stable. In Place: If an algorithm does not require an extra memory space except for few memory units, then the algorithm is said to be in place.

Sorting Algorithms
Various sorting algorithms are Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort

Bubble Sort
This is one of the most simplest sorting technique and most straight forward method of sorting. In this sorting technique, the adjacent elements in the list are compared and exchanged if they are out of order. This is also called as Sinking Sort

Time Complexity(Bubble sort)


The time complexity of bubble sort in all three cases (best, average & worst)is
T(n) = (n^2)

Which is not efficient compare to other sorting techniques.

An Animated Example
N to_do index 8 7 did_swap true

98 1

23 2

45 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 1 did_swap false

98 1

23 2

45 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 1 Swap did_swap false

98 1

23 2

45 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 1 Swap did_swap true

23 1

98 2

45 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 2 did_swap true

23 1

98 2

45 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 2 Swap did_swap true

23 1

98 2

45 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 2 Swap did_swap true

23 1

45 2

98 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 3 did_swap true

23 1

45 2

98 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 3 Swap did_swap true

23 1

45 2

98 3

14 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 3 Swap did_swap true

23 1

45 2

14 3

98 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 4 did_swap true

23 1

45 2

14 3

98 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 4 Swap did_swap true

23 1

45 2

14 3

98 4

6 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 4 Swap did_swap true

23 1

45 2

14 3

6 4

98 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 5 did_swap true

23 1

45 2

14 3

6 4

98 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 5 Swap did_swap true

23 1

45 2

14 3

6 4

98 5

67 6

33 7

42 8

An Animated Example
N to_do index 8 7 5 Swap did_swap true

23 1

45 2

14 3

6 4

67 5

98 6

33 7

42 8

An Animated Example
N to_do index 8 7 6 did_swap true

23 1

45 2

14 3

6 4

67 5

98 6

33 7

42 8

An Animated Example
N to_do index 8 7 6 Swap did_swap true

23 1

45 2

14 3

6 4

67 5

98 6

33 7

42 8

An Animated Example
N to_do index 8 7 6 Swap did_swap true

23 1

45 2

14 3

6 4

67 5

33 6

98 7

42 8

An Animated Example
N to_do index 8 7 7 did_swap true

23 1

45 2

14 3

6 4

67 5

33 6

98 7

42 8

An Animated Example
N to_do index 8 7 7 Swap did_swap true

23 1

45 2

14 3

6 4

67 5

33 6

98 7

42 8

An Animated Example
N to_do index 8 7 7 Swap did_swap true

23 1

45 2

14 3

6 4

67 5

33 6

42 7

98 8

After First Pass of Outer Loop


N to_do index 8 7 8 Finished first Bubble Up did_swap true

23 1

45 2

14 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 1 did_swap false

23 1

45 2

14 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 1 No Swap did_swap false

23 1

45 2

14 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 2 did_swap false

23 1

45 2

14 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 2 Swap did_swap false

23 1

45 2

14 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 2 Swap did_swap true

23 1

14 2

45 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 3 did_swap true

23 1

14 2

45 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 3 Swap did_swap true

23 1

14 2

45 3

6 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 3 Swap did_swap true

23 1

14 2

6 3

45 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 4 did_swap true

23 1

14 2

6 3

45 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 4 No Swap did_swap true

23 1

14 2

6 3

45 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 5 did_swap true

23 1

14 2

6 3

45 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 5 Swap did_swap true

23 1

14 2

6 3

45 4

67 5

33 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 5 Swap did_swap true

23 1

14 2

6 3

45 4

33 5

67 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 6 did_swap true

23 1

14 2

6 3

45 4

33 5

67 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 6 Swap did_swap true

23 1

14 2

6 3

45 4

33 5

67 6

42 7

98 8

The Second Bubble Up


N to_do index 8 6 6 Swap did_swap true

23 1

14 2

6 3

45 4

33 5

42 6

67 7

98 8

After Second Pass of Outer Loop


N to_do index 8 6 7 Finished second Bubble Up did_swap true

23 1

14 2

6 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 1 did_swap false

23 1

14 2

6 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 1 Swap did_swap false

23 1

14 2

6 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 1 Swap did_swap true

14 1

23 2

6 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 2 did_swap true

14 1

23 2

6 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 2 Swap did_swap true

14 1

23 2

6 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 2 Swap did_swap true

14 1

6 2

23 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 3 did_swap true

14 1

6 2

23 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 3 No Swap did_swap true

14 1

6 2

23 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 4 did_swap true

14 1

6 2

23 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 4 Swap did_swap true

14 1

6 2

23 3

45 4

33 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 4 Swap did_swap true

14 1

6 2

23 3

33 4

45 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 5 did_swap true

14 1

6 2

23 3

33 4

45 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 5 Swap did_swap true

14 1

6 2

23 3

33 4

45 5

42 6

67 7

98 8

The Third Bubble Up


N to_do index 8 5 5 Swap did_swap true

14 1

6 2

23 3

33 4

42 5

45 6

67 7

98 8

After Third Pass of Outer Loop


N to_do index 8 5 6 Finished third Bubble Up did_swap true

14 1

6 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 1 did_swap false

14 1

6 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 1 Swap did_swap false

14 1

6 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 1 Swap did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 2 did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 2 No Swap did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 3 did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 3 No Swap did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 4 did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fourth Bubble Up


N to_do index 8 4 4 No Swap did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

After Fourth Pass of Outer Loop


N to_do index 8 4 5 Finished fourth Bubble Up did_swap true

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fifth Bubble Up


N to_do index 8 3 1 did_swap false

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fifth Bubble Up


N to_do index 8 3 1 No Swap did_swap false

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fifth Bubble Up


N to_do index 8 3 2 did_swap false

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fifth Bubble Up


N to_do index 8 3 2 No Swap did_swap false

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fifth Bubble Up


N to_do index 8 3 3 did_swap false

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

The Fifth Bubble Up


N to_do index 8 3 3 No Swap did_swap false

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

After Fifth Pass of Outer Loop


N to_do index 8 3 4 Finished fifth Bubble Up did_swap false

6 1

14 2

23 3

33 4

42 5

45 6

67 7

98 8

Selection Sort
In this technique first we find the smallest item in the list and we exchange it with the first item. Next, obtain the second smallest item in the list & exchange it with the second element and so on. Since, the next least item is selected and exchanged appropriately so that elements are finally sorted ,this technique is called Selection Sort

Time Complexity(Selection sort)


The time complexity of selection sort in all three cases (best, average& worst) is
T(n) = (n^2)

Which is not efficient compare to other sorting techniques.

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

6
Largest

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

5
Largest

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

4
Largest

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

3
Largest

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

1
Comparison Data Movement Sorted

Selection Sort

2
Largest

Comparison Data Movement Sorted

Selection Sort

2
Comparison Data Movement Sorted

Selection Sort

2
Comparison Data Movement Sorted

3
DONE!

Insertion Sort
In this technique the given list is divided into two parts:sorted part(left) &unsorted part(right). The unsorted elements can be placed any of the positions in the sorted part so that elements towards left of boundary are sorted. As each item is inserted towards the sorted left part, the boundary moves to the right decreasing the unsorted list. Finally,once the boundary moves to the right most position,the elements towards the left of boundary represent the sorted list.

Time Complexity(Insertion sort)


The Time complexity of insertion sort is: Worst case: T(n) = O(n^2)

Average case:
Best case:

T(n) = (n^2)

T(n) = (n)

Insertion Sort

23

17

45

18

12

22

Insertion Sort

23
1

17
2

45
3

18
4

12
5

22
6

Insertion Sort

23
1

17
2

45
3

18
4

12
5

22
6

Insertion Sort

17
1 2

45
3

18
4

12
5

22
6

23
1 2

Insertion Sort

45
1 2 3

18
4

12
5

22
6

17
1

23
2

Insertion Sort

18
1 2 3 4

12
5

22
6

17
1

23
2

45
3 4
5 6

Insertion Sort

12
1 2 3 4 5

22
6

17
1

18
2

23
3

45
4
5 6

Insertion Sort

12
1 2 3 4 5

22
6

17
1

18
2

23
3

45
4
5 6

Insertion Sort

22
1 2 3 4 5 6

12
1

17
2

18
3

23
4

45
5 6

Insertion Sort

22
1 2 3 4 5 6

12
1

17
2

18
3

23
4

45
5 6

Insertion Sort

12
1

17
2

18
3

22
4

23
5

45
6

Merge sort
In merge sort, a given array of elements is divided into two parts. The left part of the array as well as the right part of the array is sorted recursively. Later, the sorted left part and the sorted right part are finally merged into a single sorted vector. The process of merging of two sorted vectors into a single sorted vector is called simple merge.

Time complexity(Merge sort)


The time complexity of Merge sort in all three cases (best,average&worst) is:
T(n) = (nlog2n)

Merge Sort
9 2 6 5 3 10 1 7

Merge Sort
9

2 2

10

7 8

Merge Sort
9

10

Greatest Digit comes first Place

Merge Sort
9

10

10

Merge Sort
9

10

10

Greatest Digit comes first Place

Merge Sort
9

10

10

Greatest Digit comes first Place

Merge Sort
9

10

10

Greatest Digit comes first Place

Merge Sort
9

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

Merge Sort
9

10

10

10

10

Conclusion
By comparing all the sorting techniques Merge sort time complexity is less in all three cases (time consuming is less). So, Merge sort is the most efficient technique when compare to all other sorting techniques.

THANK YOU

You might also like