Quick Sort using C

Quicksort (sometimes called partition-exchange sort) is an efficient and very fast sorting algorithm for internal sorting, 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. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. Continue reading “Quick Sort using C”

Bubble Sort using C

Bubble sort is one of the most popular sorting methods. It can be treated as a selection sort because it is based on successively selecting the smallest element, second smallest element and so on. In order to find the successive smallest elements this process relies heavily on the exchange of the adjacent elements and swaps them if they are in the wrong order.

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted.

Continue reading “Bubble Sort using C”

Straight Selection Sort using C

Selection sorting refers to a class of algorithms for sorting a list of items using comparisons. These algorithms select successively smaller or larger items from the list and add them to the output sequence. This is an improvement of the Simple Selection Sort and instead of replacing the selected element by a unique value in the i-th pass (as happens in Simple Selection Sort), the selected element is exchanged with the i-th element. Let’s assume an array “a” with “n” elements. Thus, at the beginning of the i-th pass, the first (i-1) elements of the array are those that have been selected in the previous passes. The smallest element is now searched in the remaining (n-i+1) elements. After (n-1) passes, the sorted array is completely developed in the space occupied by the original array.

Continue reading “Straight Selection Sort using C”

Simple Selection Sort using C

The simplest possible technique based on the principle of repeated selection makes use of “n” passes over an array elements. In the i-th pass, the i-th smallest element is selected from the given array and it is placed in the i-th position of a separate output array. The already selected element is not selected next time and in order to ensure it, a unique value is put in place of the selected element in the original array.

Continue reading “Simple Selection Sort using C”

Straight Insertion Sort using C

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. More details can be found here at https://en.wikipedia.org/wiki/Insertion_sort

Let’s say we have an array a, so at each i-th pass, a[i] is successively compared with a[i-1], a[i-2], etc. until an element smaller than a[i] is found or the beginning of the array is reached. Elements that are found to be greater than a[i], are moved right by one position each to make room for a[i].

The time complexity of this algorithm is O(n^2). Continue reading “Straight Insertion Sort using C”

Shuttle Sort using C

In Shuttle Sort technique for n elements in an array a, it requires n-1 passes. When i-th pass(1<=i<=n) begins, the first i elements, i.e., elements a[0] to a[i-1] have been sorted and these occupy the first i positions of the array. To insert (i+1)th element, a[i] is compared with a[i-1] and if the value of a[i] is smaller than a[i-1] then they are exchanged. In the same way the process continues until either no exchange is required or the beginning of the array is reached.

Continue reading “Shuttle Sort using C”