Unit-3 Notes Data Structure
Unit-3 Notes Data Structure
Bubble sort
Bubble Sort is the simplest sorting algorithm that works by
repeatedly swapping the adjacent elements if they are in the wrong
order. This algorithm is not suitable for large data sets as its average
and worst-case time complexity is quite high.
12 11 13 5 6
First Pass:
Initially, the first two elements of the array are compared in
insertion sort.
12 11 13 5 6
11 12 13 5 6
Second Pass:
Now, move to the next two elements and compare them
11 12 13 5 6
Both 5 and 13 are not present at their correct place so swap them
11 12 5 13 6
11 5 12 13 6
5 11 12 13 6
5 11 12 13 6
Clearly, they are not sorted, thus perform swap between both
5 11 12 6 13
5 11 6 12 13
5 6 11 12 13
Now, as we already know that merge sort first divides the whole
array iteratively into equal halves, unless the atomic values are
achieved.
Here, we see that an array of 7 items is divided into two arrays of
size 4 and 3 respectively.
Now, again find that is left index is less than the right index for both
arrays, if found yes, then again calculate mid points for both the
arrays.
Now, further divide these two arrays into further halves, until the
atomic units of the array is reached and further division is not
possible.
After dividing the array into smallest units, start merging the
elements again based on comparison of size of elements
Firstly, compare the element for each list and then combine them
into another list in a sorted manner.
Step1
Step2
Step3
Step 4
j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
i=3
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
Step 5
Step 7