-
Notifications
You must be signed in to change notification settings - Fork 0
SORTING ALGORITHMS
A sorting algorithm is one that puts elements in a list into an order.
The most frequently used orders are:
- Numerical order
- Lexicographical order.
These can either be ascending or descending.
Efficient sorting is important for optimizing the efficiency of other algorithms such as search and merge algorithms that require data to be in sorted lists.
Sorting is also useful for canonicalizing data and for producing human-readable output.
In Computer Science, canonicalization is the process of converting data that has more than one possible representation into a standard/ normal form
Formally , the output of any sorting algorithm must satisfy two conditions.
- The output must be in
monotonicorder. i.e. each element is no smaller/ larger than the previous element, according to the required order. - The output is a permutation of the input. i.e. it is a reordering yet retaining all of the original elements.
For optimum efficiency, the input data should be stored in a data structure that allows random access rather than one that allows only sequential access.
Sorting algorithms can be classified by:
-
Computational complexity. This classifies an algorithm based on its usage of resources. This gives 3 behaviors in terms of the size of the list
- Best
- Worst
- Average case
-
Memory usage
-
Recursion
Merge sort is both recursive and non recursive. -
Stability
stable algorithms maintain the relative order of records with equal keys. -
Whether or not they are a comparison sort.
-
Serial or Parallel
-
Adaptability. This determines whether the presortedness of input affects its runing time.
-
Online
online algorithms can sort a constant stream of input.
This is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest or largest element from the unsorted portion of the list and moving it to the sorted portion of the list.
ALGORITHM
- Initialize a variable
min_idxto location 0. This points to our minimum value. - Traverse the array to find the minimum element in the array.
- If any element smaller than the value pointed to by
min_idxis found, swap both values. - Increment
min_idxto point to the next element. - Repeat until the array is sorted.
Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.
The pass trough the list is repeated until no swaps are needed, which indicates the list is sorted.
It has a time complexity of O(n^2) in the worst and average cases making it inefficient for large data sets.
However, it is simple to understand and implement.
- Define a function called
bubble_sortthat takes an integer arrayarrand an integernas its parameters. - Initialize a variable called
flagwith the value ofn-1. - Start an infinite loop.
- Within the infinite loop, create a for loop that starts at index
0and ends atn-1. - Within the for loop, define two variables called
currentandnexwhich are assigned the values ofarr[i]andarr[i+1]respectively. - Compare
currentandnex: ifcurrentis greater thannex, swap them by assigningarr[i]the value ofnexandarr[i+1]the value of current. Also, increment the value offlagby 1. Ifcurrentis not greater thannex, decrement the value offlagby 1. - At the end of the for loop, check if the value of
flagis equal to 0. If it is, break out of the infinite loop. - If the value of
flagis not equal to 0, re-initialize the value of flag to n-1. - The function doesn't return anything
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void bubble_sort(int arr[], int n) {
int flag = n - 1;
while (true) {
for (int i = 0; i < n - 1; i++) {
int current = arr[i];
int nex = arr[i + 1];
if (current > nex) {
arr[i] = nex;
arr[i + 1] = current;
flag += 1;
} else {
flag -= 1;
}
}
if (!flag) {
break;
}
flag = n - 1;
}
}
int main(void)
{
int i;
int array[] = {1,2,3,10,4,5,6};
/*array length*/
int n = sizeof(array) / sizeof(array[0]);
/* print input array */
for(i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
bubble_sort(array, n);
/* print output array */
for(i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
return (0);
}
def bubble_sort(arr):
# Array length
n = len(arr)
# checks if array is sorted
flag = n - 1
# Outer loop
while True:
# Inner loop
for i in range(n - 1):
current = arr[i]
nex = arr[i + 1]
if current > nex:
#swap
arr[i], arr[i + 1] = arr[i + 1], arr[i]
flag += 1
# No swap
else:
flag -= 1
# list is sorted
if not flag:
break
# List not yet sorted
flag = n - 1
# Driver code to test above
if __name__ == "__main__":
arr = [5, 1, 4, 55, 2, 8]
print('Before: ', arr)
bubble_sort(arr)
print('After: ', arr)
Worst and Average Case Time Complexity: O(N2). The worst case occurs when an array is reverse sorted.
Best Case Time Complexity: O(N). The best case occurs when an array is already sorted.
Auxiliary Space: O(1)
The bubble sort algorithm is stable.
It is a simple sorting algorithm that builds the final sorted list one item at a time.
It iterates through the list, and for each element, it compares it with the elements on its left and finds the correct position of that element.
It is efficient for small data sets and is also useful when the input array is almost sorted.
it has a time complexity of O(n^2) in the worst and average case.
- Define a function called
insertion_sortthat takes an arrayarras its parameter. - Assign the length of the array to a variable
n. - Create a for loop that starts at index 1 and ends at
n-1. - Within the for loop, assign the value of the loop index variable to a variable
j. - Create a while loop that continues until
jis 0. - Within the while loop, compare the current element at index
jwith the element at indexj-1. - If the current element is less than the element at index
j-1, swap them and decrement the value ofjby 1. - If the current element is not less than the element at index
j-1, break out of the while loop.