Sorting in C
Sorting in C
A sorting algorithm is stable if it preserves the original order of elements with equal key values (where the key
is the value the algorithm sorts by). For example,
[1]
When the cards are sorted by value with a stable sort, the two 5s must remain in the same order in the sorted
output that they were originally in. When they are sorted with a non-stable sort, the 5s may end up in the
opposite order in the sorted output.
Bubblesort
Program
#include<stdio.h>
int main()
int a[50],n,i,j,temp;
scanf("%d",&n);
{ printf("a[%d]=",i+1);
scanf("%d",&a[i]);
for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(i=0;i<n;++i)
printf("\na[%d]=%d \n ",i,a[i]);
return 0;
Step-by-step example[edit]
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble
sort. In each step, elements written in bold are being compared. Three passes will be required.
First Pass
(51428) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
(14258) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap
them.
Second Pass
(14258) (14258)
(12458) (12458)
(12458) (12458)
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.
Third Pass
(12458) (12458)
(12458) (12458)
(12458) (12458)
(12458) (12458)
Insertion Sort
1. algorithm
o passes through each element
o everything before element is sorted
o puts element in appropriate place in sorted half of array by checking
each element starting from the back of the sorted part of the array
2. Code Methods: insertionsort
3. Worst Case O(N2) – when array is reverse sorted
4. Best Case O(N) – when array is already sorted
5. Swap Number
o = number of inversions (i.e., i < j but a[i] > a[j])
o Running Time = O(I + N)
o Notice that for a sorted array I = 0
o For a reverse array I = O(N2)
o Average Number of inversions in an array of N distinct elements is N *
(N – 1 ) / 4 = O(N2)
6. Average Case O(n2)
7. Any algorithm that sorts by exchanging adjacent elements requires
O(N2 time on average.
o Including Bubblesort and Selection Sort
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each
iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list,
and inserts it there. It repeats until no input elements remain.
Sorting is typically done in-place, by iterating up the array, growing the sorted list behind it. At each array-position, it
checks the value there against the largest value in the sorted list (which happens to be next to it, in the previous
array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct
position within the sorted list, shifts all the larger values up to make a space, and inserts into that correct position.
The resulting array after k iterations has the property where the first k + 1 entries are sorted ("+1" because the first
entry is skipped). In each iteration the first remaining entry of the input is removed, and inserted into the result at the
correct position, thus extending the result:
becomes
with each element greater than x copied to the right as it is compared against x.
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.
Program
#include<stdio.h>
int main()
int i,j,n,temp,a[30];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
j=j-1;
for(i=0;i<n;i++)
printf("\na[%d] = %d ",i+1,a[i]);
}
return 0;
Mergesort
Program
#include<stdio.h>
#include<conio.h>
int a[30],n,i;
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
int mid;
if(i<j)
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1&&j<=j2)
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two
halves, calls itself for the two halves and then merges the two sorted halves. The merge()
function is used for merging two halves. The merge(arr, l, m, r) is key process that
assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays
into one. See following C implementation for details.
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
The following diagram from wikipedia shows the complete merge sort process for an
example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can
see that the array is recursively divided in two halves till the size becomes 1. Once the
size becomes 1, the merge processes comes into action and starts merging arrays back
till the complete array is merged.
Quicksort
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
int j;
if(l<u)
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
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. Developed by Tony Hoare in 1959[1] and published in 1961,[2] it is
still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its
main competitors, merge sort and heapsort.[3][contradictory]
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. 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. It is very similar to selection sort, except that it does not always choose worst-case partition.
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.