DS - Unit V - Sorting

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Sorting Algorithms

Sorting is the process of arranging the elements of an array.

so that they can be placed either in ascending or descending order. For example,
consider an array A = {A1, A2, A3, A4, ?? An }, the array is called to be in ascending
order if element of A are arranged like A1 > A2 > A3 > A4 > A5 > ? > An .

Consider an array;

int A[10] = { 5, 4, 10, 2, 30, 45, 34, 14, 18, 9 )

The Array sorted in ascending order will be given as;

A[] = { 2, 4, 5, 9, 10, 14, 18, 30, 34, 45 }

There are many techniques by using which, sorting can be performed.

Sorting Algorithms
Sorting algorithms are described in the following table along with the
description.

SN Sorting Description
Algorithms

1 Bubble It is the simplest sort method which performs sorting


Sort by repeatedly moving the largest element to the
highest index of the array. It comprises of comparing
each element to its adjacent element and replace
them accordingly.
2. Insertion As the name suggests, insertion sort inserts each
Sort element of the array to its proper place. It is a very
simple sort method which is used to arrange the deck
of cards while playing bridge.

3. Selection Selection sort finds the smallest element in the array


Sort and place it on the first place on the list, then it finds
the second smallest element in the array and place it
on the second place. This process continues until all
the elements are moved to their correct ordering.

4. Shell Sort Shell sort is a highly efficient sorting algorithm and is


based on insertion sort algorithm. This algorithm avoids
large shifts as in case of insertion sort, if the smaller
value is to the far right and has to be moved to the far
left.This algorithm uses insertion sort on a widely spread
elements, first to sort them and then sorts the less widely
spaced elements. This spacing is termed as interval.

5. Radix Sort In Radix sort, the sorting is done as we do sort the


names according to their alphabetical order. It is the
lenear sorting algorithm used for Inegers.
1. BUBBLE SORT :
• Bubble sort works on the repeatedly swapping of adjacent elements until
they are not in the intended order.
• It is called bubble sort because the movement of array elements is just
like the movement of air bubbles in the water.
• Bubbles in water rise up to the surface; similarly, the array elements in
bubble sort move to the end in each iteration.

Although it is simple to use, it is primarily used as an educational tool because


the performance of bubble sort is poor in the real world.
Dis Advantage: It is not suitable for large data sets. The average and worst-case
complexity of Bubble sort is O(n2), where n is a number of items.
Algorithm
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

Pseudocode
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.
Pseudocode of BubbleSort algorithm can be written as follows −
procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:


swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */


if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if

end for

/*if no number was swapped that means


array is sorted now, break the loop.*/

if(not swapped) then


break
end if

end for

end procedure return list

Working of Bubble sort Algorithm


Now, let's see the working of Bubble sort Algorithm.

To understand the working of bubble sort algorithm, let's take an


unsorted array. We are taking a short and accurate array, as we know
the complexity of bubble sort is O(n2).

Let the elements of array are -


First Pass
Sorting will start from the initial two elements. Let compare them to check
which is greater.

Here, 32 is greater than 13 (32 > 13), so it is already sorted. Now, compare
32 with 26.

Here, 26 is smaller than 36. So, swapping is required. After swapping new
array will look like -

Now, compare 32 and 35.

Here, 35 is greater than 32. So, there is no swapping required as they are
already sorted.

Now, the comparison will be in between 35 and 10.

Now, the comparison will be in between 35 and 10.

Here, 10 is smaller than 35 that are not sorted. So, swapping is required. Now,
we reach at the end of the array. After first pass, the array will be -
Now, move to the second iteration.

Second Pass
The same process will be followed for second iteration.

Here, 10 is smaller than 32. So, swapping is required. After swapping,


the array will be -

Now, move to the third iteration.

Third Pass
The same process will be followed for third iteration.

Here, 10 is smaller than 26. So, swapping is required. After swapping,


the array will be -
Now, move to the fourth iteration.

Fourth pass
Similarly, after the fourth iteration, the array will be -

Hence, there is no swapping required, so the array is completely sorted.

Bubble sort 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

1. Best Case O(n)

2. Average Case O(n2)

3. 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 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

o The space complexity of bubble sort is O(1). It is because, in bubble sort, an


extra variable is required for swapping.

o The space complexity of optimized bubble sort is O(2). It is because two extra
variables are required in optimized bubble sort.

Now, let's discuss the optimized bubble sort algorithm.


Program: Write a program to implement bubble sort in Java.

1. public class Bubble {


2. static void print (int a[]) //function to print array
elements
3. {
4. int n = a.length;
5. int i;
6. for (i = 0; i < n; i++)
7. {
8. System.out.print(a[i] + " ");
9. }
10. }
11. static void bubbleSort (int a[]) // function to
implement bubble sort
12. {
13. int n = a.length;
14. int i, j, temp;
15. for (i = 0; i < n; i++)
16. {
17. for (j = i + 1; j < n; j++)
18. {
19. if (a[j] < a[i])
20. {
21. temp = a[i];
22. a[i] = a[j];
23. a[j] = temp;
24. }
25. }
26. }
27. }
28. public static void main(String[] args) {
29. int a[] = {35, 10, 31, 11, 26};
30. Bubble b1 = new Bubble();
31. System.out.println("Before sorting array eleme
nts are - ");
32. b1.print(a);
33. b1.bubbleSort(a);
34. System.out.println();
35. System.out.println("After sorting array elemen
ts are - ");
36. b1.print(a);
37.
38. }
39. }

Output
2. Insertion Sort Algorithm

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 same approach is applied in insertion sort. The idea behind the
insertion sort is that first take one element, iterate it through the sorted
array.
Although it is simple to use, it is not appropriate for large data sets.
Time complexity of insertion sort in the average case and worst case is
O(n2), where n is the number of items.
Insertion sort is less efficient than the other sorting algorithms like heap
sort, quick sort, merge sort, etc.

Insertion sort has various advantages such as -

o Simple implementation
o Efficient for small data sets
o Adaptive, i.e., it is appropriate for data sets that
are already substantially sorted.

Now, let's see the algorithm of insert


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

Pseudocode

procedure insertionSort( A : array of items )


int holePosition
int valueToInsert

for i = 1 to length(A) inclusive do:

/* select value to be inserted */


valueToInsert = A[i]
holePosition = i

/*locate hole position for the element to be inserted */


while holePosition > 0 and A[holePosition-1] > valueToInsert
do:
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while

/* insert the number at hole position */


A[holePosition] = valueToInsert

end for

end procedure

Working of Insertion sort Algorithm


Now, let's see the working of the insertion sort Algorithm.

To understand the working of the insertion sort algorithm, let's take an


unsorted array. It will be easier to understand the insertion sort via an
example.

Let the elements of array are -

Initially, the first two elements are compared in insertion sort.

Here, 31 is greater than 12. That means both elements are already in
ascending order. So, for now, 12 is stored in a sorted sub-array.

Now, move to the next two elements and compare them.


Here, 25 is smaller than 31. So, 31 is not at correct position. Now, swap 31
with 25. Along with swapping, insertion sort will also check it with all elements
in the sorted array.

For now, the sorted array has only one element, i.e. 12. So, 25 is greater than
12. Hence, the sorted array remains sorted after swapping.

Now, two elements in the sorted array are 12 and 25. Move forward to the
next elements that are 31 and 8.

Both 31 and 8 are not sorted. So, swap them.

After swapping, elements 25 and 8 are unsorted.

So, swap them.

Now, elements 12 and 8 are unsorted.


So, swap them too.

Now, the sorted array has three items that are 8, 12 and 25. Move to the next
items that are 31 and 32.

Hence, they are already sorted. Now, the sorted array includes 8, 12, 25 and
31.

Move to the next elements that are 32 and 17.

17 is smaller than 32. So, swap them.

Swapping makes 31 and 17 unsorted. So, swap them too.

Now, swapping makes 25 and 17 unsorted. So, perform swapping again.


Now, the array is completely sorted.

Insertion sort complexity

Now, let’s see the time complexity of insertion sort in best case, average case,
and in worst case. We will also see the space complexity of insertion sort.

1. 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).

2. Space Complexity

Space Complexity O(1)

Stable YES

The space complexity of insertion sort is O(1). It is because, in insertion sort,


an extra variable is required for swapping.
Implementation of insertion sort
Program: Write a program to implement insertion sort in Java.

public class Insert

void insert(int a[]) /* function to sort an aay with insertion sort */

int i, j, temp;

int n = a.length;

for (i = 1; i < n; i++)

{ temp = a[i];

J = i – 1;

While(j>=0 && temp <= a[j]) /* Move the elements greater than temp
to one position ahead from their current position*/

A[j+1] = a[j];

J = j-1;

A[j+1] = temp;

Void printArr(int a[]) /* function to print the array */

int i;
int n = a.length;

for (i = 0; i < n; i++)

System.out.print(a[i] + “ “);

public static void main(String args[])

{ int a[] = { 92, 50, 5, 20, 11, 22 };

Insert i1 = new Insert();

System.out.println(“\nBefore sorting array elements are – “);

i1.printArr(a);

i1.insert(a);

System.out.println(“\n\nAfter sorting array elements are – “);

i1.printArr(a);

System.out.println();

Output:
3. 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.

Now, let us learn some programming aspects of 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

Pseudocode
procedure selection sort
list : array of items
n : size of list

for i = 1 to n - 1
/* set current element as minimum*/
min = i

/* check the element to be minimum */

for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for

/* swap the minimum element with the current element*/


if indexMin != i then
swap list[min] and list[i]
end if
end for

end procedure
How Selection Sort Works?
Consider the following depicted array as an example.

For the first position in the sorted list, the whole list is scanned sequentially. The first
position where 14 is stored presently, we search the whole list and find that 10 is the
lowest value.

So we replace 14 with 10. After one iteration 10, which happens to be the minimum value
in the list, appears in the first position of the sorted list.

For the second position, where 33 is residing, we start scanning the rest of the list in a
linear manner.

We find that 14 is the second lowest value in the list and it should appear at the second
place. We swap these values.

After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process −
4. SHELL SORT:

Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm
avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved
to the far left.

This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts the less
widely spaced elements. This spacing is termed as interval. This interval is calculated based on Knuth’s
formula as –

Knuth’s Formula

H=h*3+1

Where –

H is interval with initial value 1

This algorithm is quite efficient for medium-sized data sets as its average and worst-case complexity of
this algorithm depends on the gap sequence the best known is Ο(n), where n is the number of items.
And the worst case space complexity is O(n).

Algorithm
Following is the algorithm for shell sort.
Step 1 − Initialize the value of h
Step 2 − Divide the list into smaller sub-list of equal interval h
Step 3 − Sort these sub-lists using insertion sort
Step 3 − Repeat until complete list is sorted
Pseudocode
Following is the pseudocode for shell sort.
procedure shellSort()
A : array of items

/* calculate interval*/
while interval < A.length /3 do:
interval = interval * 3 + 1
end while

while interval > 0 do:

for outer = interval; outer < A.length; outer ++ do:

/* select value to be inserted */


valueToInsert = A[outer]
inner = outer;

/*shift element towards right*/


while inner > interval -1 && A[inner - interval] >=
valueToInsert do:
A[inner] = A[inner - interval]
inner = inner - interval
end while

/* insert the number at hole position */


A[inner] = valueToInsert

end for

/* calculate interval*/
interval = (interval -1) /3;

end while

end procedure
How Shell Sort Works?
Let us consider the following example to have an idea of how shell sort works. We take
the same array we have used in our previous examples. For our example and ease of
understanding, we take the interval of 4. Make a virtual sub-list of all values located at
the interval of 4 positions. Here these values are {35, 14}, {33, 19}, {42, 27} and {10, 44}

We compare values in each sub-list and swap them (if necessary) in the original array.
After this step, the new array should look like this −

Then, we take interval of 1 and this gap generates two sub-lists - {14, 27, 35, 42}, {19,
10, 33, 44}
We compare and swap the values, if required, in the original array. After this step, the
array should look like this –

Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion
sort to sort the array.

Following is the step-by-step depiction –


We see that it required only four swaps to sort the rest of the array.
JAVA PROGRAM:

Import java.util.*;

Public class Shell_Sort

Static void shellsort(int[] arr, intnum)

Int i, j, k, tmp;

For (i = num / 2; i> 0; i = i / 2)

For (j = i; j<num; j++)

For(k = j – i; k>= 0; k = k – i)

If (arr[k+i] >= arr[k])

Break;

Else

Tmp = arr[k];

Arr[k] = arr[k+i];

Arr[k+i] = tmp;

Public static void main(String[] args)

{
Scanner sc = new Scanner(System.in);

Int arr[]= newint[30];

Intk, num;

System.out.println(“Enter total no. Of elements : “);

Num = sc.nextInt();

For (k = 0 ; k<num; k++)

Arr[k]=sc.nextInt();

Shellsort(arr, num);

System.out.println(“\n Sorted array is: “);

For (k = 0; k<num; k++)

System.out.println(arr[k]);

Output:

Enter total no. Of elements : 6

10

Sorted array is: 1 2 2 3 4 10


5. RADIX SORT

Radix sort is the linear sorting algorithm that is used for integers.

In Radix sort, there is digit by digit sorting is performed that is started from the
least significant digit to the most significant digit.

1. radixSort(arr)

2. max = largest element in the given array

3. d = number of digits in the largest element (or, max)

4. Now, create d buckets of size 0 - 9

5. for i -> 0 to d

6. sort the array elements using counting sort (or any stable sort) according to the

digits at the ith place

working of radix sort in detail by using an example. To understand it more


clearly, let's take an unsorted array and try to sort it using radix sort. It will
make the explanation clearer and easier.

In the given array, the largest element is 736 that have 3 digits in it.

So, the loop will run up to three times (i.e., to the hundreds place). That means
three passes are required to sort the array.

Now, first sort the elements on the basis of unit place digits (i.e., x = 0). Here,
we are using the counting sort algorithm to sort the elements.
Pass 1:
In the first pass, the list is sorted on the basis of the digits at 0's place.

After the first pass, the array elements are -

Pass 2:
In this pass, the list is sorted on the basis of the next significant digits (i.e.,
digits at 10th place).
After the second pass, the array elements are -

Pass 3:
In this pass, the list is sorted on the basis of the next significant digits (i.e.,
digits at 100th place).
After the third pass, the array elements are -

Now, the array is sorted in ascending order.

Program: Write a program to implement Radix sort in Java.

1. class RadixSort {

2.

3. int getMax(int a[], int n) {

4. int max = a[0];

5. for(int i = 1; i<n; i++) {

6. if(a[i] > max)

7. max = a[i];

8. }
9. return max; //maximum element from the array

10. }

11.

12. void countingSort(int a[], int n, int place) // function to implement counting

13.

14. sort

15. {

16. int[] output = new int[n+1];

17. int[] count = new int[10];

18.

19. // Calculate count of elements

20. for (int i = 0; i < n; i++)

21. count[(a[i] / place) % 10]++;

22.

23. // Calculate cumulative frequency

24. for (int i = 1; i < 10; i++)

25. count[i] += count[i - 1];

26.

27. // Place the elements in sorted order

28. for (int i = n - 1; i >= 0; i--) {

29. output[count[(a[i] / place) % 10] - 1] = a[i];

30. count[(a[i] / place) % 10]--;

31. }

32.

33. for (int i = 0; i < n; i++)

34. a[i] = output[i];

35. }

36.

37. // function to implement radix sort


38. void radixsort(int a[], int n) {

39.

40. // get maximum element from array

41. int max = getMax(a, n);

42.

43. // Apply counting sort to sort elements based on place value

44. for (int place = 1; max / place > 0; place *= 10)

45. countingSort(a, n, place);

46. }

47.

48. // function to print array elements

49. void printArray(int a[], int n) {

50. for (int i = 0; i < n; ++i)

51. System.out.print(a[i] + " ");

52. }

53.

54. public static void main(String args[]) {

55. int a[] = {151, 259, 360, 91, 115, 706, 34, 858, 2};

56. int n = a.length;

57. RadixSort r1 = new RadixSort();

58. System.out.print("Before sorting array elements are - \n");

59. r1.printArray(a,n);

60. r1.radixsort(a, n);

61. System.out.print("\n\nAfter applying Radix sort, the array elements are -

62.

63. \n");

64. r1.printArray(a, n);

65. }

66. }
Output:

You might also like