You are on page 1of 26

Sorting

• Sorting takes an unordered collection and makes


it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6

5 12 35 42 77 101
Bubble Sort
Selection Sort
Insertion Sort
Sorting Quick Sort
Heap Sort
Merge Sort
Radix Sort
Bubble Sort: "Bubbling Up" the Largest
Element
• 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
"Bubbling Up" the Largest Element
• 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

7742 Swap4277 35 12 101 5


"Bubbling Up" the Largest Element
• 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 7735 Swap 3577 12 101 5


"Bubbling Up" the Largest Element
• 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 7712 Swap 12
77 101 5
"Bubbling Up" the Largest Element
• 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
"Bubbling Up" the Largest Element
• 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 1015 Swap 101


5
"Bubbling Up" the Largest Element
• 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


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


Repeat “Bubble Up” How Many
Times?
• If we have N elements…

• And if each time we bubble an element, we


place it in its correct location…

• Then we repeat the “bubble up” process N – 1


times.

• This guarantees we’ll correctly


place all N elements.
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
Reducing the Number of Comparisons
• On the Nth “bubble up”, we only need to
do MAX-N comparisons.

• For example:
• This is the 4th “bubble up”
• MAX is 6
• Thus we have 2 comparisons to do
1 2 3 4 5 6
12 35 5 42 77 101
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
Complexity
Selection Sort
• Selection sort is a simple sorting algorithm. This sorting algorithm is
an in-place comparison-based algorithm in which the list is divided
into two parts, the sorted part at the left end and the unsorted part at
the right end. Initially, the sorted part is empty and the unsorted part
is the entire list.
• The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element becomes a part
of the sorted array. This process continues moving unsorted array
boundary by one element to the right.
• This algorithm is not suitable for large data sets as its average and
worst case complexities are of Ο(n2), where n is the number of
items.
Working
• Let A be an array with n elements. Then, selection sort algorithm used
for sorting is as follows-
• Here,
• i = variable to traverse the array A
• index = variable to store the index of minimum element
• j = variable to traverse the unsorted sub-array
• temp = temporary variable used for swapping
• Consider the following elements are to be sorted in ascending order-
6, 2, 11, 7, 5
Complexity
• Also, we can analyze the complexity by simply observing the number of loops.
There are 2 loops so the complexity is n*n=n2.
• Worst Case Complexity O(n2) : If we want to sort in ascending order and the
array is in descending order then, the worst case occurs.
• Best Case Complexity O(n2) : It occurs when the array is already sorted
• Average Case Complexity O(n2) : It occurs when the elements of the array are in
jumbled order (neither ascending nor descending).
• The time complexity of the selection sort is the same in all cases. At every step,
you have to find the minimum element and put it in the right place. The
minimum element is not known until the end of the array is not reached.
• Space Complexity:
Space complexity is O(1) because an extra variable temp is used
Insertion Sort
• This sort works on the principle of inserting an element at a particular position,
hence the name Insertion Sort.
1.The first step involves the comparison of the element in question with its
adjacent element.
2.And if at every comparison reveals that the element in question can be inserted
at a particular position, then space is created for it by shifting the other elements
one position to the right and inserting the element at the suitable position.
3.The above procedure is repeated until all the element in the array is at their apt
position.
• Let us now understand
working with the following
example:
Consider the following
array: 25, 17, 31, 13, 2
• First Iteration: Compare 25
with 17. The comparison
shows 17< 25. Hence
swap 17 and 25.
The array now looks like:
17, 25, 31, 13, 2
• Second Iteration: Begin with the second
element (25), but it was already swapped
on for the correct position, so we move
ahead to the next element.
• Now hold on to the third element (31) and
compare with the ones preceding it.
• Since 31> 25, no swapping takes place.
• Also, 31> 17, no swapping takes place
and 31 remains at its position.
• The array after the Second iteration looks
like:
• 17, 25, 31, 13, 2
• Third Iteration: Start the
following Iteration with the
fourth element (13), and
compare it with its preceding
elements.
• Since 13< 31, we swap the
two.
• Array now becomes: 17, 25,
13, 31, 2.
• But there still exist elements
that we haven’t yet compared
with 13. Now the comparison
takes place between 25 and
13. Since, 13 < 25, we swap
the two.
• The array becomes 17, 13,
25, 31, 2.
• The last comparison for the
iteration is now between 17
and 13. Since 13 < 17, we
swap the two.
• The array now becomes 13,
17, 25, 31, 2.
• Fourth Iteration: The last iteration calls for the
comparison of the last element (2), with all the
preceding elements and make the appropriate
swapping between elements.
• Since, 2< 31. Swap 2 and 31.
• Array now becomes: 13, 17, 25, 2, 31.
• Compare 2 with 25, 17, 13.
• Since, 2< 25. Swap 25 and 2.
• 13, 17, 2, 25, 31.
• Compare 2 with 17 and 13.
• Since, 2<17. Swap 2 and 17.
• Array now becomes:
• 13, 2, 17, 25, 31.
• The last comparison for the Iteration is to
compare 2 with 13.
• Since 2< 13. Swap 2 and 13.
• The array now becomes:
• 2, 13, 17, 25, 31.
• This is the final array after all the corresponding
iterations and swapping of elements.
Pseudocode and Complexity

Even though insertion sort is efficient, still, if we provide an


already sorted array to the insertion sort algorithm, it will
still execute the outer for loop, thereby requiring n steps to
sort an already sorted array of n elements, which makes
its best case time complexity a linear function of n.
Wherein for an unsorted array, it takes for an element to
compare with all the other elements which mean every n
element compared with all other n elements. Thus, making
it for n x n, i.e., n2 comparisons.

You might also like