Algorithms

Sorting Algorithms

In computer science a sorting algorithm is an algorithm that puts elements of a list in a certain order.



Lightest and Heaviest

Almost any list that comes out of a computer is sorted into some sort of order, and there are many more sorted lists inside computers that the user doesn't see. Many clever algorithms have been devised for putting values into order efficiently.

Fig.1:Insertion Sort


Selection Sort

randerson112358

Sort the following array using the selection sort algorithm. In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort.

Download PDF


Types of Sorting Algorithms Sort Types

Bubble Sort - Bubble sort, sometimes referred to as sinking 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 through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.

Merge Sort - Merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.

Conceptually, a merge sort works as follows:
1)Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
2)Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.

Selection Sort - selection sort is a sorting algorithm, specifically an in-place comparison sort. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Heap Sort- heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a heap data structure rather than a linear-time search to find the maximum.Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more favorable worst-case O(n log n) runtime.
The heapsort algorithm involves preparing the list by first turning it into a max heap. The algorithm then repeatedly swaps the first value of the list with the last value, decreasing the range of values considered in the heap operation by one, and sifting the new first value into its position in the heap. This repeats until the range of considered values is one value in length.

The steps are:
1) Call the buildMaxHeap() function on the list. Also referred to as heapify(), this builds a heap from a list in O(n) operations.
2)Swap the first element of the list with the final element. Decrease the considered range of the list by one.
3)Call the siftDown() function on the list to sift the new first element to its appropriate index in the heap.
4)Go to step (2) unless the considered range of the list is one element.

QuickSort-Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort. Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined.Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.

The steps are:
1) Pick an element, called a pivot, from the array.
2) Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
3) Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.

The base case of the recursion is arrays of size zero or one, which never need to be sorted.

Insertion Sort-Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

1) Simple implementation: Bentley shows a three-line C version, and a five-line optimized version.
2) Efficient for (quite) small data sets, much like other quadratic sorting algorithms
3) More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort
4) Adaptive, i.e., efficient for data sets that are already substantially sorted: the time complexity is O(nk) when each element in the input is no more than k places away from its sorted position
5) Stable; i.e., does not change the relative order of elements with equal keys
6) In-place; i.e., only requires a constant amount O(1) of additional memory space
7) Online; i.e., can sort a list as it receives it
When people manually sort cards in a bridge hand, most use a method that is similar to insertion sort.

The most common variant of insertion sort, which operates on arrays, can be described as follows:

1) Suppose there exists a function called Insert designed to insert a value into a sorted sequence at the beginning of an array. It operates by beginning at the end of the sequence and shifting each element one place to the right until a suitable position is found for the new element. The function has the side effect of overwriting the value stored immediately after the sorted sequence in the array.

2)To perform an insertion sort, begin at the left-most element of the array and invoke Insert to insert each element encountered into its correct position. The ordered sequence into which the element is inserted is stored at the beginning of the array in the set of indices already examined. Each insertion overwrites a single value: the value being inserted.

An example of the Selection Sort Algorithm:

An example of the Merge Sort Algorithm:

An example of the Bubble Sort Algorithm:

An example of the Heap Sort Algorithm:

An example of the Quick Sort Algorithm:

An example of the Insertion Sort Algorithm:


Bubble Sort C Program

computer science (compsci112358)

This program sorts an array of elements using the bubble sort algorithm

Output: Enter total number(s) of elements: 4 Enter the 4 elements: 1 5 4 3 After Sorting: 1 3 4 5

►Support this channel : https://www.amazon.com/gp/product/0131103628/ref=as_li_tl?ie=UTF8&tag=everythingc06-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=0131103628&linkId=c6b8768d5fe382a757ab85da5c1686ae

Get the code here:
https://github.com/randerson112358/C-Programs/blob/master/BubbleSort.c


Insertion Sort C Program

computer science (compsci112358)

This program sorts an array of elements using the insertion sort algorithm

Output: Enter total elements: 3 Enter 3 elements: 9 4 8 After Sorting: 4 8 9

►Support this channel : https://www.amazon.com/gp/product/0131103628/ref=as_li_tl?ie=UTF8&tag=everythingc06-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=0131103628&linkId=c6b8768d5fe382a757ab85da5c1686ae

Get the code here:
https://github.com/randerson112358/C-Programs/blob/master/InsertionSort.c


Selection Sort C Program

computer science (compsci112358)

This program sorts an array of elements using the Selection sort algorithm

Output: Enter total number(s) of elements: 4 Enter the 4 elements: 1 5 4 3 After Sorting: 1 3 4 5

►Support this channel : https://www.amazon.com/gp/product/0131103628/ref=as_li_tl?ie=UTF8&tag=everythingc06-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=0131103628&linkId=c6b8768d5fe382a757ab85da5c1686ae

Get the code here:
https://github.com/randerson112358/C-Programs/blob/master/SelectionSort.c


/*
   This program sorts an array of elements using the insertion sort algorithm
   By:randerson112358
   
   Output:
   Enter total elements: 3
   Enter 3 elements: 9 4 8
   After Sorting: 4 8 9
*/

#include < stdio.h >

int insertionSort(int size, int *array);

int main(){
	
	int size, i , array[21];
	
	printf("Enter total number of elements: ");
	scanf("%d", &size);
	
	printf("Enter %d elements: ", size);
	for(i=0; i < size; i++)
	   scanf("%d", &array[i]);
	   
	//Start sorting the array using the insertion sort algorithm
	insertionSort(size, array);
	
	printf("After Sorting: ");
	for(i=0; i < size; i++)
	   printf(" %d", array[i]);
	
	printf("\n");
	system("pause"); //Comment this our if you are not using a Windows OS
	return 0;
}

int insertionSort(int size, int *array){
	
	int i, j;
	int temp;
	
	//Insertion Sort
	for(i=1; i < size; i++){
		
		temp=array[i];
		j= i-1;
		
		while( (temp < array[j]) && (j >= 0)){
			
			//swap
			array[j+1] = array[j];
			j= j-1;
		}
		
		array[j+1] = temp;
	}
	
	return 1;
}



Copyright © 2013, Everything Computer Science.