0% found this document useful (0 votes)
61 views8 pages

Elementary Sorting Techniques

The document discusses three sorting algorithms: 1. Bubble sort compares adjacent elements and swaps them if out of order with a complexity of O(n^2). 2. Insertion sort maintains a sorted sub-list, inserts elements into their proper place with a complexity of O(n^2). 3. Merge sort follows a divide and conquer approach, divides the array into halves, sorts them recursively, and merges the sorted halves together with a complexity of O(n log n).

Uploaded by

Ummer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views8 pages

Elementary Sorting Techniques

The document discusses three sorting algorithms: 1. Bubble sort compares adjacent elements and swaps them if out of order with a complexity of O(n^2). 2. Insertion sort maintains a sorted sub-list, inserts elements into their proper place with a complexity of O(n^2). 3. Merge sort follows a divide and conquer approach, divides the array into halves, sorts them recursively, and merges the sorted halves together with a complexity of O(n log n).

Uploaded by

Ummer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Hina Gojwari

CASET College

(Elementary Sorting Techniques)

1. Bubble Sort Algorithm

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. This
algorithm is not suitable for large data sets as its average and worst
case complexity are of Ο (n2).

How Bubble Sort Works?

We take an unsorted array for our example.

Bubble sort starts with very first two elements, comparing them to check
which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted


locations. Next, we compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be


swapped.
Hina Gojwari
CASET College

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted
positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.

We swap these values. We find that we have reached the end of the
array. After one iteration, the array should look like this −

To be precise, we are now showing how an array should look like after
each iteration. After the second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is
completely sorted.
Hina Gojwari
CASET College

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

Why the name bubble sort?

The movement of an element in an array during one iteration of bubble


sort algorithm is similar to the movement of an air bubble that raises up
in the water.

Complexity

Complexity Best case Average Case Worst Case

Time Complexity O(n) O(n2) O(n2)


Hina Gojwari
CASET College

2. Insertion Sort Algorithm

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.

This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο (n2).

Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to
the elements before. Move the greater elements one position up to
make space for the swapped element.
Hina Gojwari
CASET College

Example:

Another Example:

12, 11, 13, 5, 6


Let us loop for i = 1 (second element of the array) to 4 (last element of the
array)
Hina Gojwari
CASET College

i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12


11, 12, 13, 5, 6

i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller


than 13
11, 12, 13, 5, 6
i = 3. 5 will move to the beginning and all other elements from 11 to 13
will move one position ahead of their current position.
5, 11, 12, 13, 6

i = 4. 6 will move to position after 5, and elements from 11 to 13 will move


one position ahead of their current position.
5, 6, 11, 12, 13

Insertion sort is a simple sorting algorithm that works similar to the way
you sort playing cards in your hands. The array is virtually split into a
sorted and an unsorted part. Values from the unsorted part are picked
and placed at the correct position in the sorted part.

Complexity

Complexity Best case Average Case Worst Case

Time Complexity O(n) O(n2) O(n2)


Hina Gojwari
CASET College

2.Merge Sort Algorithm

Merge sort is the algorithm which follows divide and conquer approach.
Consider an array A of n number of elements. The algorithm processes
the elements in 3 steps.

1. If A Contain 1 element then it is already sorted, otherwise, Divide A


into two sub-array of equal number of elements.
2. Conquer means sort the two sub-arrays recursively using the
merge sort.
3. Combine the sub-arrays to form a single final sorted array
maintaining the ordering of the array.

The main idea behind merge sort is that, the short list takes less time to
be sorted.

Complexity

Complexity Best case Average Case Worst Case

Time Complexity O(n log n) O(n log n) O(n log n)

Algorithm

MergeSort(arr[ ], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = l+ (r-1)/2

2. Call mergeSort for first half:


Call mergeSort(arr, l, m)

3. Call mergeSort for second half:


Call mergeSort(arr, m+1, r)

4. Merge the two halves sorted in step 2 and 3:


Call merge(arr, l, m, r)
Hina Gojwari
CASET College

The following example shows the complete merge sort process for an
example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the
diagram, we can see that the array is recursively divided into two halves till
the size becomes 1. Once the size becomes 1, the merge processes come
into action and start merging arrays back till the complete array is merged.

Example:

You might also like