Unit5 Sort
Unit5 Sort
Introduction
Sort: the process through which data are arranged according to their values.
Sorts Types : Internal or External sorts.
For example, a file of 20,000 records may be sorted using an array that
holds only 1000 records. During the sorting process, only 1000 records
are therefore in memory at any one time; the other 19,000 are kept in a
file in secondary storage.
Sort Order, Stability & Efficiency
The sort order identifies the sequence of the sorted data, ascending or
descending.
If the order of the sort is not specified, it is assumed to be ascending
Sort stability is an attribute of a sort, indicating that data with equal
keys maintain their relative input order in the output.
the straight insertion sort and the bubble sort are stable; the others are unstable.
The algorithm repeatedly selects the smallest (or largest) element from the unsorted
portion of the list and swaps it with the first element of the unsorted part.
This process is repeated for the remaining unsorted portion until the entire list is sorted.
Selection Sort (straight and heap)
Algorithm
SELECTION SORT(arr, n)
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.
In the straight insertion sort, the list is divided into two parts: sorted
and unsorted. In each pass the first element of the unsorted sublist is
transferred to the sorted sublist by inserting it at the appropriate
place.
Insertion Sort(Straight Insertion, Shell Sort)
Steps:
• start with second element of the array as
first element in the array is assumed to be
sorted.
• Compare second element with the first
element and check if the second element
is smaller then swap them.
• Move to the third element and compare it
with the second element, then the first
element and swap as necessary to put it in
the correct position among the first three
elements.
• Continue this process, comparing each
element with the ones before it and
swapping as needed to place it in the
correct position among the sorted
elements.
• Repeat until the entire array is sorted.
.
Bubble Sort
Bubble Sort is a simple algorithm which is used to sort a given set of n 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.
f 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.
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.
Following are the Time and Space complexity for the Bubble Sort algorithm.
Worst Case Time Complexity [ Big-O ]: O(n2)
Best Case Time Complexity [Big-omega]: O(n)
Average Time Complexity [Big-theta]: O(n2)
Space Complexity: O(1)
Shell Sort :
• This is an Improvement over
Simple
method Insertion
theSort .In this Method The
Element at Fixed Distance K (K is Preferably
prime Number ) or compared .
• The distance will then be decremented by
Some fixed amount and again the Comparison
will be made . Finally Individuals elements
will be compared.
9
10
11
Shell sort
In the third loop, elements are lying at the interval of 1 (n/8 = 1),
where n = 8
Quick Sort
Choosing the
pivot
Complexity
Quick sort?
It is dividing elements in to smaller parts based on some condition and performing the
sort operations on those divided smaller parts. Hence, it works well for large datasets.
So, here are the steps how Quick sort works in simple words.
4. Pivot element can be any element from the array, it can be the first element, the last
element or any random element. In this eg., we will take the first element or the last
element as pivot.
Quick sort working
Radix Sort :
• Radix Sort is one of the Linear Sorting
algorithm for Integers.
• It is generated from radix Sort.
• It can be performed using Bucket 0 to
9.
27
• It is also called as Binsort.
• In First Pass all element arranged according to
the least Significant digit. In Second Pass ,the
element are arranged according to the next
least significant digit and so on.
28
29
Merge sort
• Merge sort is a sorting algorithm that follows the divide-and-conquer approach.
• It works by recursively dividing the input array into smaller subarrays and sorting
those subarrays then merging them back together to obtain the sorted array.
• In simple terms, we can say that the process of merge sort is to divide the array into
two halves, sort each half, and then merge the sorted halves back together. This
process is repeated until the entire array is sorted.
Merge Sort
Complexity Analysis of Merge Sort
Merge Sort is quite fast, and has a time complexity of O(n*log n). It is also a stable sort,
which means the "equal" elements are ordered in the same order in the sorted list.
As we have already learned in Binary Search that whenever we divide a number into half
in every step, it can be represented using a logarithmic function, which is log n and the
number of steps can be represented by log n + 1(at most)
Also, we perform a single step operation to find out the middle of any subarray, i.e. O(1).
And to merge the subarrays, made by dividing the original array of n elements, a running
time of O(n) will be required.
Hence the total time for mergeSort function will become n(log n + 1), which gives us a
time complexity of O(n*log n).
Worst Case Time Complexity [ Big-O ]: O(n*log n)
Time complexity of Merge Sort is O(n*Log n) in all the 3 cases (worst, average and best)
as merge sort always divides the array in two halves and takes linear time to merge two
halves.
It requires equal amount of additional space as the unsorted array. Hence its not at all
recommended for searching large unsorted arrays.
It is the best Sorting technique used for sorting Linked Lists.
External Sort
Merging Ordered Files
A merge is the process that combines two files sorted on a given key into one sorted file on
the same given key.
Merging Unordered Files