You are on page 1of 32

DSA with Java/Unit- 8

Unit 8: Sorting
 The efficiency of data handling can often be substantially increased if the data are sorted
according to some criteria of order. For example, it would be practically impossible to find
a name in the telephone directory if the names were not alphabetically ordered. The same
can be said about dictionaries, book indexes, payrolls, bank accounts, student lists, and
other alphabetically organized materials.
 The convenience of using sorted data is unquestionable and must be addressed in computer
science as well. Although a computer can grapple with an unordered telephone book more
easily and quickly than a human can, it is extremely inefficient to have the computer
process such an unordered data set.
 It is often necessary to sort data before processing
Sorting is the process of arranging data in particular order (ascending or descending ).
 The first step is to choose the criteria that will be used to order data. This choice will vary
from application to application and must be defined by the user.
o Very often, the sorting criteria are natural, as in the case of numbers.
o A set of numbers can be sorted in ascending or descending order.
o The set of five positive integers (5, 8, 1, 2, 20) can be sorted in ascending order
resulting in the set (1, 2, 5, 8, 20) or in descending order resulting in the set (20, 8,
5, 2, 1).
o Names in the phone book are ordered alphabetically by last name, which is the
natural order.
o For alphabetic and nonalphabetic characters, the American Standard Code for
Information Interchange (ASCII) code is commonly used, although other choices
such as Extended Binary Coded Decimal Interchange Code (EBCDIC) are possible.
 Once a criterion is selected, the second step is how to put a set of data in order using that
criterion.
 The final ordering of data can be obtained in a variety of ways, and only some of them can
be considered meaningful and efficient. ( We can consider Space and Time Complexities)

1
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Elementary Sorting Algorithms


 Elementary sorting algorithms are very simple sorting algorithms. Their average case
performance is O( n2 ). Hence they are not recommended for very large data sets, but they
are useful for small data sets because of their simplicity.
 All elementary sorting algorithms work by dividing the array to be sorted into two sub-
arrays:
o A sub-array of unsorted elements.
o A sub-array of sorted elements.
The algorithm repeatedly moves an element from the unsorted sub-array to its proper
position in the sorted sub-array. In this way the sorted sub-array expands on the expense
of the unsorted sub-array, until the sorted sub-array encompasses all elements
 Following are the examples of elementary sorting algorithms
 Insertion Sort
 Selection Sort
 Bubble Sort
Insertion Sort
 Insertion sort is based on the idea that one element from the input elements is consumed in
each iteration to find its correct position i.e, the position to which it belongs in a sorted
array.
 It iterates the input elements by growing the sorted array at each iteration. It compares the
current element with the largest value in the sorted array. If the current element is greater,
then it leaves the element in its place and moves on to the next element else it finds its
correct position in the sorted array and moves it to that position. This is done by shifting
all the elements, which are larger than the current element, in the sorted array to one
position ahead (this for ascending order)
 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 inserted in this sorted sub-list, has to find its appropriate place
and then it has to be inserted there. Hence the name, insertion sort.

2
Collected by Bipin Timalsina
DSA with Java/Unit- 8

 Insertion sort algorithm arranges a list of elements in a particular order. In insertion sort
algorithm, every iteration moves an element from unsorted portion to sorted portion until
all the elements are sorted in the list.
 Insertion sort is a simple sorting algorithm that works similar to the way you sort playing
cards in your hands.
Insertion Sort algorithm to sort an array (data [] ) of size n in ascending order:
1. Iterate from data [1] to data[n-1] over the array.
2. Compare the current element (tmp) to its predecessor.
3. If the tmp element is smaller than its predecessor, compare it to the elements
before it (left of it) and
 move the greater element one position up (right) to make space for the
swapped element.
An outline of the insertion sort algorithm is as follows:

Implementation of insertion sort for an array of integer can be done using following java
method
public void insertionSort(int[] data){
for(int i = 1; i < data.length; i++){
int tmp = data[i];
int j = i - 1;
while(j >= 0 && data[j] > tmp){
data[j + 1] = data[j];
j = j - 1;
}
data[j + 1] = tmp;
}
}

3
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Example:
Consider the following elements are to be sorted in ascending order
85, 12, 59, 45, 72, 51
The process of insertion sort is illustrated as follow:

Complexity of insertion sort


 Selection sort algorithm consists of two nested loops. Owing to the two nested loops, it has
O(n2) time complexity.
 Space Complexity: O(1)
Selection Sort
 This algorithm gets its name from the way it iterates through the array: it selects the current
smallest element, and swaps it into place.
 In selection sort, the smallest value among the unsorted elements of the array is selected in
every pass and inserted to its appropriate position into the array.

4
Collected by Bipin Timalsina
DSA with Java/Unit- 8

 First, find the smallest element of the array and place it on the first position. Then, find the
second smallest element of the array and place it on the second position. The process
continues until we get the sorted array.
 The array with n elements is sorted by using n-1 pass of selection sort 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.
Following are the steps involved in selection sort (for sorting a given array in ascending
order):
1. Starting from the first element, we search the smallest element in the array, and replace it
with the element in the first position.
2. We then move on to the second position, and look for smallest element present in the
subarray, starting from index 1, till the last index.
3. We replace the element at the second position in the original array, or we can say at the
first position in the subarray, with the second smallest element.
4. This is repeated, until the array is completely sorted.

Following pseudocode represents the procedure in selection sort

5
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Example:

6
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Implementation of selection sort for an array of integer can be done using following java
method
void selectionSort(int dada[]) {
int n = data.length;
for(int i=0; i<n; i++) {
int min_idx = i;

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


// Select the minimum element in each pass.
if(data[j] < data[min_idx])
min_idx = j;
}
// put minimum at the correct position
int temp = data[min_idx];
data[min_idx] = data[i];
data[i] = temp;
}
}

Time Complexity:
The time complexity of selection sort is O(n²) in all cases even if the whole array is sorted because
the entire array need to be iterated for every element and it contains two nested loops.
Space Complexity: O(1)

Bubble Sort
 Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are
swapped if they are not in order.
 In Bubble sort, each element of the array is compared with its adjacent element. The
algorithm processes the list in passes.
 A list with n elements requires n-1 passes for sorting.
 The “bubble” sort is called so because the list elements with greater value than their
surrounding elements “bubble” towards the end of the list. For example, after first pass,
the largest element is bubbled towards the right most position. After second pass, the
second largest element is bubbled towards the second last position in the list and so on.

7
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Following are the steps involved in bubble sort (for sorting a given array in ascending order):
1. Starting with the first element (index = 0), compare the current element with the next
element of the array.
2. If the current element is greater than the next element of the array, swap them.
3. If the current element is less than the next element, move to the next element.
4. Repeat from step1 until all the elements are sorted.
Following pseudocode represents the procedure in bubble sort
bubblesort(data[])
n= data.length
for i = 0 to n-2
for j = 0 to n–i-1
swap elements in position j and j+1 if they are out
of order;

Example:

8
Collected by Bipin Timalsina
DSA with Java/Unit- 8

9
Collected by Bipin Timalsina
DSA with Java/Unit- 8

10
Collected by Bipin Timalsina
DSA with Java/Unit- 8

11
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Implementation of selection sort for an array of integer can be done using following java
method

void bubblesort(int data[]) {


int n = data.length;
int temp;
for(int i=0; i<n; i++) {
for(int j=0; j<n-i-1; j++) {
if(data[j]>data[j+1]) {
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
}

Complexity:
 Time complexity of Bubble Sort is O(n2).
 The space complexity for Bubble Sort is O(1)

Efficient Sorting Algorithms


 The algorithms whose average time complexity is less than that of elementary sorting
algorithms (less than O(n2))
 Some of the efficient sorting algorithms
o Heap Sort
o Quicksort
o Mergesort
o Radix Sort

Heap Sort
 Selection sort makes O(n2) comparisons and is very inefficient, especially for large n. But
it performs relatively few moves. If the comparison part of the algorithm is improved, the
end results can be promising.
 Heap sort was invented by John Williams and uses the approach inherent to selection sort.
 To have the array sorted in ascending order, heap sort puts the largest element at the end
of the array, then the second largest in front of it, and so on. Heap sort starts from the end

12
Collected by Bipin Timalsina
DSA with Java/Unit- 8

of the array by finding the largest elements, whereas selection sort starts from the beginning
using the smallest elements. The final order in both cases is indeed the same.
 Heaps can be used in sorting an array. In max heaps, maximum element is at the root and
in min heaps minimum element is at the root. Heap Sort uses this property of heap to sort
the array.
 Heap sort algorithm is divided into two basic parts:
 Creating a Heap of the unsorted list/array.
 Then a sorted array is created by repeatedly removing the largest/smallest element
from the heap, and inserting it into the array. The heap is reconstructed after each
removal.
Process in heap sort to sort the elements of an array, data[] in ascending order.
1. Initially build a max heap of elements in the array.
2. The root element, that is data[0], contains maximum element of data. After that, swap
this element with the last element of data and heapify the max heap excluding the last
element which is already in its correct position and then decrease the size of the heap
by one.
3. Repeat the step 2, until all the elements are in their correct position.
Note: For descending order, Min heaps should be used.
Pseudo code

13
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Example:

Converting into max heap

14
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Swapping the Root and Last Elements

Restoring the Heap Property

15
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Repeating the Steps

16
Collected by Bipin Timalsina
DSA with Java/Unit- 8

We repeat the process until there is only one element left in the tree:

Complexity
 Time Complexity : O( n log n)
 Space Complexity: O (1)

Java Code
Following java method, heapsort() can be used to implement heapsort to sort elements of an integer
array in ascending order. The heapsort() method uses heapify() method to restore heap property.

17
Collected by Bipin Timalsina
DSA with Java/Unit- 8

static void heapsort(int data[]) {


int n = data.length;
int temp;

for(int i = n/2; i >= 0; i--) {


heapify(data, n-1, i);
}

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


//swap last element of the max-heap with the first element
temp = data[i];
data[i] = data[0];
data[0] = temp;

//exclude the last element from the heap and rebuild the heap
heapify(data, i-1, 0);
}
}

// heapify function is used to build the max heap


// max heap has maximum element at the root which means
// first element of the array will be maximum in max heap
static void heapify(int data[], int n, int i) {
int max = i;
int left = 2*i + 1;
int right = 2*i + 2;

//if the left element is greater than root


if(left <= n && data[left] > data[max]) {
max = left;
}

//if the right element is greater than root


if(right <= n && data[right] > data[max]) {
max = right;
}

//if the max is not i


if(max != i) {
int temp = data[i];
data[i] = data[max];
data[max] = temp;
//Recursively heapify the affected sub-tree
heapify(data, n, max);
}
}

18
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Quick sort
 Based on divide and conquer approach.
 Quick sort is also known as partition exchange sort.
 The main idea of Quick sort is partitioning an array about an element called pivot.
 An array A[ ] is partitioned about an pivot element A[i] if all the elements to the left of
A[i] are less than or equal to A[i] and all the elements to the right of A[i] are greater than
A[i].
A[] ={5,9,6,8,10,14,11,16,19,15}
Here array A is partitioned about A[4] =10
But A is not partitioned about A[5] = 14
 The partitioning step of Quick Sort rearrange the array so that the pivot element is placed
on it’s proper position on the sorted list and all the elements to the left are smaller or equal
to pivot and elements to the right are greater than pivot.
 There are many different versions of QuickSort that pick pivot in different ways.( first
element, last element, median element, random element, etc can be chosen as pivot )
Outline of Quick Sort Algorithm (pivot = first element )
 Take the first element of array as pivot
 Set Left marker ‘L’ at the beginning of array and right Marker ‘R’ at the end of the array.
 While L< R, Repeat
o Increment L (move right) until the element at index position is less than or equal
to pivot element
o Decrement R (move left) until the element at index position is greater than pivot
element
o If (L< R)
 Swap (A[L], A[R]
 Swap pivot with A[R]
 Return R

19
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Pseudo code of Quick Sort Algorithm and Partition


QuickSort(A,p,q){
if (p<q){
j = Partition(A,p,q);
QuickSort(A,p,j-1);
QuickSort(A,j+1,q);
}
}
Partition(A, i, j){
L = i;
R= j;
pivot = A[i];
while (L< R){
while(A[L] < = pivot && L< = j){
L++ ;
}
while(A[R] > pivot){
R--;
}
if(L< R){
swap (A[L], A[R]);
}
}
swap (pivot, A[R]);
return R;
}

20
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Example: Sorting following array A, using Quick Sort

 Here, pivot = 25 , L and R are pointing at beginning and end of the array
0 1 2 3 4 5
25 10 30 15 20 28
L R
 Now Since L<R and L< = j(5), Increase L until the element is < = 25 and decrease R until
element is > 25
0 1 2 3 4 5
25 10 30 15 20 28
L R
Here, L<R so, swap (A[L], A[R]) i.e. swap 30 and 20
0 1 2 3 4 5
25 10 20 15 30 28
L R
 Again increase L and Decrease R
0 1 2 3 4 5
25 10 20 15 30 28
R L
Here, L>=R so, swap pivot and A[R]
0 1 2 3 4 5
15 10 20 25 30 28
R L
Partition is complete around pivot 25.
0 1 2 3 4 5
15 10 20 25 30 28

Left_sub_array[ ] = {15,10,20} and Right sub_array = {30,28 }


 The process is repeated on subarrays also.

21
Collected by Bipin Timalsina
DSA with Java/Unit- 8

 For the left subarray pivot = 15, L=0, R = 20 similarly, for right subarray pivot =30,
L=4,R=5

0 1 2 3 4 5
15 10 20 25 30 28
L R L R

 Now Increasing L and decreasing R on both subarrays .( In right subarray, R is not


decreased since A[R] < pivot)
0 1 2 3 4 5
15 10 20 25 30 28
R L RL
 In left subarray Swap A[R] and pivot i.e.15. In right subarray also swap A[R] and pivot i.e.
30. As a result, left subarray is partitioned around 15 and right subarray is partitioned
around 30. There are single elements in both sides of 15, which therefore are sorted. We
can clearly see that 28 is single element in left of 30 which is also sorted. Hence all elements
now are sorted.
0 1 2 3 4 5
10 15 20 25 28 30

Time complexity: Best case: O(n lg n )when the array is split approximately halves.
Worst case: O(n2 )
Space complexity: O(n)

22
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Merge Sort
 Based on Divide and Conquer approach.
 Conceptually, a merge sort works as follows:
o Divide the unsorted list into n sublists, each containing one element (a list of one
element is considered sorted).
o Repeatedly merge sublists to produce new sorted sublists until there is only one
sublist remaining. This will be the sorted list.
 Merge sort is an efficient sorting technique which is based on divide and conquer paradigm.
 In this technique, an array of size n is recursively divided into two sub arrays of size n/2
until each of sub list size becomes 1. After complete divide process this method applies
merging the two sub arrays recursively to obtain the single sorted list.
 The process of merging is combining the two sorted arrays into single sorted arrays. So,the
process of merge sort involves following steps:
o Divide the array into two subarrays recursively
o Recursively sort the two subarrays
o Combine two sorted subarrays into a single sorted array

 Pseudo Codes for mergesort and merge are given below:

23
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Algorithm for Merge Sort


mergeSort(A, low, high){
if(high>low){
// Find the middle point to divide the array into two halves
mid = (low+high)/2;
//Call mergeSort for first half
mergeSort(A, low, mid);
//Call mergeSort for second half
mergeSort(A, mid+1, high);
//Merge the two halves sorted in above steps
merge(A, low, mid, high);
}
}

Algorithm for Merge


Merge(A, low, mid, high){
i = low; j = mid+1;
for(k = low; k<=high;k++){
if(i > mid){
B[k] = A[j];
j = j+1;

24
Collected by Bipin Timalsina
DSA with Java/Unit- 8

}
else if(j > high){
B[k] = A[i];
i= i+1;
}
else if(A[i] <A[j] ){
B[k] = A[i];
i= i+1;
}
else{
B[k] = A[j];
j = j+1;
}
}
for(k= low; k<=high;k++){
A[k] = B[k];
}
}

Example of merging procedure


Given two sorted subarrays of an array A are {1, 5, 10, 30, 50} and {3, 11, 60, 80}
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

i = low mid j high


0 1 2 3 4 5 6 7 8
B[]=
k

25
Collected by Bipin Timalsina
DSA with Java/Unit- 8

When k=0; A[i] < A[j] so, B[k] = A[i]; i++;


0 1 2 3 4 5 6 7 8
B[]= 1

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

i mid j
Now k =1; A[i]> A[j] so, B[k] = A[j]; j++;

0 1 2 3 4 5 6 7 8
B[]= 1 3

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

i mid j

Now, k=2; A[i] < A[j] so, B[k] = A[i]; i++;


0 1 2 3 4 5 6 7 8
B[]= 1 3 5

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

i mid j
Now k=3; A[i] < A[j] so, B[k] = A[i]; i++;

26
Collected by Bipin Timalsina
DSA with Java/Unit- 8

0 1 2 3 4 5 6 7 8
B[]= 1 3 5 10

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

i mid j
When k =4; Here, A[i]> A[j] so, B[k] = A[j]; j++;
0 1 2 3 4 5 6 7 8
B[]= 1 3 5 10 11

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

i mid j

When k =5; Here, A[i] < A[j] so, B[k] = A[i]; i++;
0 1 2 3 4 5 6 7 8
B[]= 1 3 5 10 11 30

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

i mid j
When k =6; Here, also A[i] < A[j] so, B[k] = A[i]; i++;
0 1 2 3 4 5 6 7 8
B[]= 1 3 5 10 11 30 50

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

mid i j

27
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Now k =7; Here, i > mid so, B[k] = A[j]; j++;


0 1 2 3 4 5 6 7 8
B[]= 1 3 5 10 11 30 50 60

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

mid i j
When k =8; Here, i>mid so, B[k] = A[j]; j++;
0 1 2 3 4 5 6 7 8
B[]= 1 3 5 10 11 30 50 60 80

k
0 1 2 3 4 5 6 7 8
A[]= 1 5 10 30 50 3 11 60 80

mid i j
Here k = high; so merging completed.
Now copying contents of temporary array B to the Original array A. Here the array is also sorted
after combining sorted subarrays.
0 1 2 3 4 5 6 7 8
A[]= 1 3 5 10 11 30 50 60 80

Process of merge sort is illustrated in following figure

28
Collected by Bipin Timalsina
DSA with Java/Unit- 8

29
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Time Complexity of merge sort: O(n lg n)


Space Complexity merge sort: O(n)

Radix sort
 Radix sort is a popular way of sorting used in everyday life. To sort library cards, we may
create as many piles of cards as letters in the alphabet, each pile containing authors whose
names start with the same letter. Then, each pile is sorted separately using the same method;
namely, piles are created according to the second letter of the authors’ names. This process
continues until the number of times the piles are divided into smaller piles equals the
number of letters of the longest name.
 When sorting integers, 10 piles numbered 0 through 9 are created, and initially, integers
are put in a given pile according to their rightmost digit so that 93 is put in pile 3. Then,
piles are combined and the process is repeated, this time with the second rightmost digit;
in this case, 93 ends up on pile 9. The process ends after the leftmost digit of the longest
number is processed.
 Radix sort is a sorting technique that sorts the elements by first grouping the individual
digits of the same place value. Then, sort the elements according to their
increasing/decreasing order.
 Radix sort is a non-comparative sorting algorithm.
Pseudocode

The Radix sort algorithm for integers is performed using the following steps.
 Step 1 - Define 10 queues each representing a bucket/ piles for each digit from 0 to 9.
 Step 2 - Consider the least significant digit of each number in the list which is to be sorted.
 Step 3 - Insert each number into their respective queue based on the least significant digit.
 Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted into
their respective queues.

30
Collected by Bipin Timalsina
DSA with Java/Unit- 8

 Step 5 - Repeat from step 3 based on the next significant digit.


 Step 6 - Repeat from step 2 until all the numbers are grouped based on the most significant
digit.
Example: Sorting the list 10, 1234, 9, 7234, 67, 9181, 733, 197, 7, 3 with radix sort

31
Collected by Bipin Timalsina
DSA with Java/Unit- 8

Note: Numbers of passes required = number of digits in the largest element in the list
For example, if the largest number is a 3 digit number then that list is sorted with 3 passes.

Java Function to implement Radix Sort


//Here radix =10 for integers in decimal number system
// digits = numbers of digits in the maximum number in the list

Time complexity: O(d * n )


Space complexity: O(d +n )
Here d is the number of bits required to store each element of the list and n is the size of the list

Case Study: Adding Polynomials


(please refer the text book)

32
Collected by Bipin Timalsina

You might also like