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

Sorting Algorithms

about SA

Uploaded by

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

Sorting Algorithms

about SA

Uploaded by

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

Sorting

Sorting is nothing but arranging the data in ascending or descending order. The term sorting came into
picture, as humans realised the importance of searching quickly.

There are so many things in our real life that we need to search for, like a particular record in database, roll
numbers in merit list, a particular telephone number in telephone directory, a particular page in a book etc.
All this would have been a mess if the data was kept unordered and unsorted, but fortunately the concept
of sorting came into existence, making it easier for everyone to arrange data in an order, hence making it
easier to search.

Sorting arranges data in a sequence which makes searching easier.

The two main criterias to judge which algorithm is better than the other have been:

• Time taken to sort the given data.


• Memory Space required to do so.
Bubble Sort
Bubble Sort is a simple algorithm which is used to sort a given set of elements provided in form of an array
with n number of elements. Bubble Sort compares all the element one by one and sort them based on their
values.

If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first
element of the array with the second element, if the first element is greater than the second element, it will
swap both the elements, and then move on to compare the second and the third element, and so on.

If we have total n elements, then we need to repeat this process for n-1 times.

It is known as bubble sort, because with every complete iteration the largest element in the given array,
bubbles up towards the last place or the highest index, just like a water bubble rises up to the water surface.

Sorting takes place by stepping through all the elements one-by-one and comparing it with the adjacent
element and swapping them if required.
Bubble Sort Algorithm
Following are the steps involved in bubble sort(for sorting a given array in ascending order):

• Starting with the first element(index = 0), compare the current element with the next element of the
array.

• If the current element is greater than the next element of the array, swap them.

• If the current element is less than the next element, move to the next element. Repeat Step 1.

Let the elements of an array are


Here 32 is greater than 26 (32 > 26), so swapping is required. Swap 32 and 26.
We assume list is an array of n elements. We further assume that swap function swaps the values of the given
array elements.

begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort
We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array is
completely sorted in an ascending order. This may cause a few complexity issues like what if the array needs
no more swapping as all the elements are already ascending.

To ease-out the issue, we use one flag variable swapped which will help us see if any swap has happened or
not. If no swap has occurred, i.e. the array requires no more processing to be sorted, it will come out of the
loop.
procedure bubbleSort(array)

n = length(array)
repeat
swapped = false
for i = 1 to n - 1
if array[i - 1] > array[i], then
swap(array[i - 1], array[i])
swapped = true
end if
end for
n=n-1
until not swapped
end bubbleSort
Bubble Sort Time Complexity
Now, let's see the time complexity of bubble sort in the best case, average case, and worst case. We will also
see the space complexity of bubble sort.

Time Complexity

Case Time Complexity

Best Case O(n)


2
Average Case O(n )
2
Worst Case O(n )
Best Case Complexity - It occurs when there is no sorting required, i.e. the array is already sorted. The
best-case time complexity of bubble sort is O(n).
Average Case Complexity - It occurs when the array elements are in jumbled order that is not properly
ascending and not properly descending. The average case time complexity of bubble sort is O(n2).
Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse order. That
means suppose you have to sort the array elements in ascending order, but its elements are in descending
order. The worst-case time complexity of bubble sort is O(n2).

Space Complexity

Space Complexity O(1)


Stable YES

• The space complexity of bubble sort is O(1). It is because, in bubble sort, an extra variable is required for
swapping.
• The space complexity of optimized bubble sort is O(2). It is because two extra variables are required in
optimized bubble sort.
Insertion Sort
• This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted.
For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this
sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion
sort.

• Insertion sort works similar to the sorting of playing cards in hands. It is assumed that the first card is already
sorted in the card game, and then we select an unsorted card. If the selected unsorted card is greater than the
first card, it will be placed at the right side; otherwise, it will be placed at the left side. Similarly, all unsorted
cards are taken and put in their exact place.

• The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the
same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of
Ο(n2), where n is the number of items.
Insertion Sort Algorithm
The simple steps of achieving the insertion sort are listed as follows -

Step 1 - If the element is the first element, assume that it is already sorted. Return 1.

Step2 - Pick the next element, and store it separately in a key.

Step3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current element, then move to the next element.
Else, shift greater elements in the array towards the right.

Step 5 - Insert the value.

Step 6 - Repeat until the array is sorted.


Example
Lets consider the following Array
0 1 2 3 4 5
N=6
5 3 2 1 4 2

Initially, we set first element as the sorted list.


0 1 2 3 4 5

5 3 2 1 4 2

Now begin with the unsorted list, pick the first element and keep it in the variable “Key. Now we
will try to find its appropriate position in the sorted sublist.
0 1 2 3 4 5 Key

5 3 2 1 4 2 3
Now compare the Key with the last element in the sorted sublist (right to left), i.e. 5. Since 3<5, it should be
placed before 5. So we need space before 5 to place 3. Therefore, 5 is moved to one position ahead, i.e. A[i+1]
= A[i].
0 1 2 3 4 5 Key

5 5 2 1 4 2 3
We will have to compare the Key with all the elements of the sorted sublist (right to left), but since we have a
single element in the sorted sublist, no more comparison is required. Now we can place the Key element at
index 0.
0 1 2 3 4 5

3 5 2 1 4 2
Now the sorted sublist will have 3 and 5

0 1 2 3 4 5

3 5 2 1 4 2
Now again begin with the unsorted list (from left to right), pick the first element and keep it in the
variable “Key”. Now we will try to find its appropriate position in the sorted sublist.

0 1 2 3 4 5 Key

3 5 2 1 4 2 2
Now compare the Key with the last element in the sorted sublist (right to left), i.e. 5. Since 2<5, it should be
placed before 5. So we need space before 5 to place 2. Therefore, 5 is moved to one position ahead, i.e. A[i+1]
= A[i].
0 1 2 3 4 5 Key

3 5 5 1 4 2 2

Next, we will compare the key with the next element in the sorted list until we find the smaller element than
key. The next element is 3 and key<3, So we need space before 3 to place 2. Therefore, 3 is moved to one
position ahead, i.e. A[i+1] = A[i].
0 1 2 3 4 5 Key

3 3 5 1 4 2 2

Now the key can be inserted to index 0 as no more element is there in the sorted list.

0 1 2 3 4 5

2 3 5 1 4 2

Now the sorted list will be as below.

0 1 2 3 4 5

2 3 5 1 4 2
Now again begin with the unsorted list (from left to right), pick the first element and keep it in the
variable “Key”. Now we will try to find its appropriate position in the sorted sublist.

0 1 2 3 4 5 Key

2 3 5 1 4 2 1
Now compare the Key with the last element in the sorted sublist (right to left), i.e. 5. Since 1<5, it should be
placed before 5. So we need space before 5 to place 1. Therefore, 5 is moved to one position ahead, i.e. A[i+1]
= A[i].

0 1 2 3 4 5
Key
2 3 5 5 4 2 1
Next, we will compare the key with the next element in the sorted list until we find the smaller element than
key. The next element is 3 and key<3, So we need space before 3 to place 1. Therefore, 3 is moved to one
position ahead, i.e. A[i+1] = A[i].
0 1 2 3 4 5 Key

2 3 3 5 4 2 1

Next, we will compare the key with the next element in the sorted list. The next element is 2 and key<2, So we
need space before 2 to place 1. Therefore, 2 is moved to one position ahead, i.e. A[i+1] = A[i].

0 1 2 3 4 5 Key

2 2 3 5 4 2 1

Now the key can be inserted to index 0 as no more element is there in the sorted list.
0 1 2 3 4 5

1 2 3 5 4 2
0 1 2 3 4 5
Now the sorted list will be as below.
1 2 3 5 4 2
Now again begin with the unsorted list (from left to right), pick the first element and keep it in the
variable “Key”. Now we will try to find its appropriate position in the sorted sublist.

0 1 2 3 4 5 Key

1 2 3 5 4 2 4
Now compare the Key with the last element in the sorted sublist (right to left) until we find the smaller
element than key, i.e. 5. Since 4<5, it should be placed before 5. So we need space before 5 to place 4.
Therefore, 5 is moved to one position ahead, i.e. A[i+1] = A[i].
Key
0 1 2 3 4 5

1 2 3 5 5 2 4

Next, we will compare the key with the next element in the sorted list. The next element is 3 and key>3, we
will stop comparing the key and place it on index 3. Now the sorted list will be
0 1 2 3 4 5

1 2 3 4 5 2
Now again begin with the unsorted list (from left to right), pick the first element and keep it in the
variable “Key”. Now we will try to find its appropriate position in the sorted sublist.

0 1 2 3 4 5 Key
Key<5 2
1 2 3 4 5 2

0 1 2 3 4 5 Key

1 2 3 4 5 5 Key <4 2

0 1 2 3 4 5 Key

1 2 3 4 4 5 Key <3 2

0 1 2 3 4 5 Key
No shift
1 2 3 3 4 5 Key =2 2
0 1 2 3 4 5 Key

1 2 3 3 4 5 2

0 1 2 3 4 5

1 2 2 3 4 5

Now the sorted list will be

0 1 2 3 4 5

1 2 2 3 4 5
Insertion Sort Algorithm
INSERTION-SORT(A)
for i = 1 to n
key ← A [i]
j←i–1
while j > = 0 and A[j] > key
A[j+1] ← A[j]
j←j–1
End while
A[j+1] ← key
End for
Insertion Sort Time Complexity

Time Complexity

Case Time Complexity

Best Case O(n)


Average Case O(n2)
Worst Case O(n2)
• Best Case Complexity - It occurs when there is no sorting required, i.e. the array is already sorted. The
best-case time complexity of insertion sort is O(n).
• Average Case Complexity - It occurs when the array elements are in jumbled order that is not properly
ascending and not properly descending. The average case time complexity of insertion sort is O(n2).
• Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse order.
That means suppose you have to sort the array elements in ascending order, but its elements are in
descending order. The worst-case time complexity of insertion sort is O(n2).

Space Complexity

• The space complexity of insertion sort is O(1). It is because, in insertion sort, an extra variable is required
for swapping.

Space Complexity O(1)


Stable YES
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 best, average and worst case complexities are of Ο(n2),
where n is the number of items.

Selection sort is generally used when -


• A small array is to be sorted
• Swapping cost doesn't matter
• It is compulsory to check all elements
Selection Sort Algorithm

Step 1 − Set MIN to location 0


Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
The same process is applied to the rest of the array elements.
Selection Sort Pseudocode
SELECTION SORT(arr, n)

Step 1: Repeat Steps 2 and 3 for i = 0 to n-1


Step 2: CALL SMALLEST(arr, i, n, pos)
Step 3: SWAP arr[i] with arr[pos]
[END OF LOOP]
Step 4: EXIT

SMALLEST (arr, i, n, pos)


Step 1: SET SMALL = arr[i]
Step 2: SET pos = i
Step 3: Repeat for j = i+1 to n
if (SMALL > arr[j])
SET SMALL = arr[j]
SET pos = j
[END OF if]
[END OF LOOP]
Step 4: RETURN pos
Selection Sort Time Complexity

Time Complexity

Case Time Complexity


Best Case O(n2)
Average Case O(n2)
Worst Case O(n2)
• Best Case Complexity - The best-case time complexity of selection sort is O(n2).

• Average Case Complexity - It occurs when the array elements are in jumbled order that is not properly
ascending and not properly descending. The average case time complexity of selection sort is O(n2).

• Worst Case Complexity - The worst-case time complexity of selection sort is O(n2).

Space Complexity

• The space complexity of selection sort is O(1). It is because, in selection sort, an extra variable is required
for swapping.

Space Complexity O(1)


Stable YES

You might also like