Wa0034.
Wa0034.
The importance of sorting lies in the fact that data searching can be optimized to a very high level,
if data is stored in a sorted manner. Sorting is also used to represent data in more readable formats.
Following are some of the examples of sorting in real-life scenarios −
• Telephone Directory − the telephone directory stores the telephone numbers of people
sorted by their names, so that the names can be searched easily.
• Dictionary − the dictionary stores words in an alphabetical order so that searching of any
word becomes easy.
Sorting algorithms may require some extra space for comparison and temporary storage of few
data elements. These algorithms do not require any extra space (in memory) and sorting is
said to happen in-place called as internal sorting. For example, within the array itself.
This is called in-place sorting.
[https://www.tutorialspoint.com/data_structures_algorithms]
Sorting Efficiency
There are many techniques for sorting. Implementation of particular sorting technique depends upon
situation. Sorting techniques mainly depends on two parameters. First parameter is the execution
time of program, which means time taken for execution of program. Second is the space, which
means space, taken by the program?
Sorting Techniques
List sorting techniques?
1. Selection sort
2. Insertion sort
3. Bubble sort
4. Radix sort
5. Quick sort
1. Selection sort
Selection sorting is conceptually the most simplest sorting algorithm. This algorithm first finds
the smallest element in the array and exchanges it with the element in the first position, then find the
second smallest element and exchange it with the element in the second position, and continues in
this way until the entire array is sorted.
How Selection Sorting Works
In the first pass, the smallest element found is 1, so it is placed at the first position, then leaving first
element, smallest element is searched from the rest of the elements, 3 is the smallest, so it is then
placed at the second position. Then we leave 1 and 3, from the rest of the elements, we search for the
smallest and put it at third position and keep doing this, until array is sorted.
Sorting using Selection Sort Algorithm
Advantages
i. The main advantage of the selection sort is that it performs well on a small list.
ii. It is an in-place sorting algorithm; no additional temporary storage is required beyond
what is needed to hold the original list.
Disadvantages
i. The primary disadvantage of the selection sort is its poor efficiency when dealing with a huge list
of items.
ii. Its performance is easily influenced by the initial ordering of the items before the sorting process.
Complexity Analysis of Selection Sorting
Worst case time complexity: O(n2)
Best case time complexity: O(n2)
Average case time complexity: O(n2)
Space complexity: O(1)
2. Insertion sort:
It is a simple Sorting algorithm which sorts the array by shifting elements one by one. Following
are some of the important characteristics of Insertion Sort.
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
return 0;
}
Advantages of insertion sort:
1. simplicity and efficiency of the insertion sort, especially for small arrays.
Disadvantages of insertion sort:
1. It is less efficient on list containing more number of elements.
2. As the number of elements increases the performance of the program would be slow.
3. Insertion sort needs a large number of element shifts.
Complexity Analysis of Selection Sorting
Worst case time complexity: O(n2)
Best case time complexity: O(n)
Average case time complexity: O(n2)
Space complexity: O(1)
3) 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. This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2) where n is the number of items.
How Bubble sort works
We take an unsorted array for our example. Bubble sort takes Ο(n 2) time so we're keeping it short
and precise.
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.
Next we compare 33 and 35. We find that both are in already sorted positions.
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.
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
Advantages
i. The bubble sort requires very little memory other than that which the array or list itself occupies.
ii. The bubble sort is comprised of relatively few lines of code.
iii. With a best-case running time of O(n), the bubble sort is good for testing whether or not a list is
sorted or not. Other sorting methods often cycle through their whole sorting sequence, which often
have running times of O(n^2) or O(n log n) for this task.
iv. The same applies for data sets that have only a few items that need to be swapped a few times.
Disadvantages
i. The main disadvantage of the bubble sort method is the time it requires.
ii. With a running time of O (n^2), it is highly inefficient for large data sets.
The array becomes : 10,11,17,21,34,44,123,654 which is sorted. This is how our algorithm works.
Advantages:
1.Fast when the keys are short i.e. when the range of the array elements is less.
2. Used in suffix array construction.
Disadvantages:
1. Since Radix Sort depends on digits or letters, Radix Sort is much less flexible than other sorts.
Hence, for every different type of data it needs to be rewritten.
2. The constant for Radix sort is greater compared to other sorting algorithms.
3. It takes more space compared to Quicksort which is inplace sorting.
5) Quick Sort:
Quick Sort, as the name suggests, sorts any list very quickly. Quick sort is not stable search, but it is
very fast and requires very less additional space. It is based on the rule of Divide and Conquer (also
called partition-exchange sort). This algorithm divides the list into three main parts:
In the list of elements, mentioned in below example, we have taken 25 as pivot. So after the first
pass, the list will be changed like this.
6 8 17 14 25 63 37 52
Hence after the first pass, pivot will be set at its position, with all the elements smaller to it on its left
and all the elements larger than it on the right. Now 6 8 17 14 and 63 37 52 are considered as two
separate lists, and same logic is applied on them, and we keep doing this until the complete list is
sorted.
How Quick Sorting Works
Quick Sort Pivot Algorithm
In Quick sort algorithm, partitioning of the list is performed using following steps...
Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in the list).
Step 2 - Define two variables i and j. Set i and j to first and last elements of the list respectively.
Advantages:
Applications:
• Space required by quick sort is very less, only O(n log n) additional space is required.
• Quick sort is not a stable sorting technique, so it might change the occurrence of two similar
elements in the list while sorting.
2.2 Searching
Search is a process of finding a value in a list of values. In other words, searching is the process of
locating given value position in a list of values.
Linear search algorithm finds given element in a list of elements with O(n) time complexity
where n is total number of elements in the list. This search process starts comparing of search
element with the first element in the list. If both are matching then results with element found
otherwise search element is compared with next element in the list. If both are matched, then the
result is "element found". Otherwise, repeat the same with the next element in the list until search
element is compared with last element in the list, if that last element also doesn't match, then the
result is "Element not found in the list".
Example
Consider the following list of element and search element...
The array to be sorted is as follows:
21 35 41 65 72
It is sorted in ascending order. Now let key = 40. At first 21 is checked as x[0]=21.
It is smaller than 40. So next element is checked which is 35 that is also smaller than 40. So now 41 is checked.
But 41 > 40 & all elements to the right of 41 are also greater than 40.So we terminate the search as an
unsuccessful one and we may not have to search the entire list.
Complexity:
Searching is NOT more efficient when key is in present in the list in case when the search key value lies between
the minimum and the maximum element in the list. The Complexity of linear search both in case of sorted and
unsorted list is the same. The average complexity for linear search for sorted list is better than that in unsorted list
since the search need not continue beyond an element with higher value than the search value.
In case of unsorted list, we have to search the entire list every time i.e.we have to keep on searching the list till
we find the required element or we reach the end of the list.this is because as elements are not in any order,so any
element can be found just anywhere.
Algorithm:
Complexity:
The number of comparisons in this case is n-1.So it is of o(n). The implementation af algo is simple but the
efficiency is not good. Every time we have to search the whole array(if the element with required value is not
found out).
#include<stdio.h>
#include<conio.h>
void main(){
int list[20],size,i,sElement;
printf("Enter size of the list: ");
scanf("%d",&size);
Advantage
i. This is the simplest searching algorithm
ii. The space complexity is very less.
iii. Efficient for small data, if the dataset created is small.
Disadvantage
i. The time complexity increases.
ii. The number of comparisons become large as n, for the large set of array.
2. Binary Search:
The most efficient method of searching a sequential file is binary search. This method is applicable
to elements of a sorted list only. In this method, to search an element we compare it with the center
element of the list. If it matches, then the search is successful and it is terminated. But if it does not
match, the list is divided into two halves. The first half consists of 0th element to the center element
whereas the second list consists of the element next to the center element to the last element. Now It
is obvious that all elements in first half will be < or = to the center element and all element elements
in the second half will be > than the center element. If the element to be searched is greater than the
center element then searching will be done in the second half, otherwise in the first half.
Same process of comparing the element to be searched with the center element & if not found then
dividing the elements into two halves is repeated for the first or second half. This process is
repeated till the required element is found or the division of half parts gives a single element.
Illustrative Explanation:
Let the array to be sorted is as follows:
11 23 31 33 65 68 71 89 100
So again low= 1 + 1 = 2.
Similarly had the key been 32 it would have been an unsuccessful search.
Complexity:
This is highly efficient than linear search. Each comparison in the binary search reduces the no. of possible
candidates by a factor of 2. So the maximum no. of key comparisons is equal to log(2,n) approx. So the complexity
of binary search is O(log n).
Limitations:
Binary search algorithm can only be used if the list to be searched is in array form and not linked list. This is
bcause the algorithm uses the fact that the indices of the array elements are consecutive integers. This makes this alg
orithm useless for lists with many insertions and deletions which can be implemented only when the list is in the form
of a linked list.