Sorting is one of the most basic functions applied to data. It means arranging the data in a particular fashion, which can be increasing or decreasing. There is a builtin function in C++ STL by the name of sort().
This function internally uses IntroSort. In more details it is implemented using hybrid of QuickSort, HeapSort and InsertionSort.By default, it uses QuickSort but if QuickSort is doing unfair partitioning and taking more than N*logN time, it switches to HeapSort and when the array size becomes really small, it switches to InsertionSort.
The prototype for sort is :
sort(startaddress, endaddress)
startaddress: the address of the first
element of the array
endaddress: the address of the next
contiguous location of the
last element of the array.
So actually sort() sorts in the
range of [startaddress,endaddress)
Simple Example:
C++
#include <algorithm>
#include <iostream>
int main()
{
int arr[] = {3, 5, 1, 2, 4};
std::sort(std::begin(arr), std::end(arr));
for ( int i : arr)
{
std::cout << i << " " ;
}
return 0;
}
|
C++
#include <algorithm>
#include <iostream>
using namespace std;
void show( int a[], int array_size)
{
for ( int i = 0; i < array_size; ++i)
cout << a[i] << " " ;
}
int main()
{
int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int asize = sizeof (a) / sizeof (a[0]);
cout << "The array before sorting is : \n" ;
show(a, asize);
sort(a, a + asize);
cout << "\n\nThe array after sorting is :\n" ;
show(a, asize);
return 0;
}
|
OutputThe array before sorting is :
1 5 8 9 6 7 3 4 2 0
The array after sorting is :
0 1 2 3 4 5 6 7 8 9
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Refer std::sort() for more details.
quick sort code:
C++
#include <iostream>
using namespace std;
int partition( int arr[], int start, int end)
{
int pivot = arr[start];
int count = 0;
for ( int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);
int i = start, j = end;
while (i < pivotIndex && j > pivotIndex) {
while (arr[i] <= pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort( int arr[], int start, int end)
{
if (start >= end)
return ;
int p = partition(arr, start, end);
quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
}
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = 10;
quickSort(arr, 0, n - 1);
cout<< "array after using quick sort: " <<endl;
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
return 0;
}
|
Outputarray after using quick sort:
0 1 2 3 4 5 6 7 8 9
average case Time Complexity: O(N*logN)
worst case Time Complexity: O(N^2)
Auxiliary Space: O(1)
Heap sort code:
C++
#include <iostream>
using namespace std;
void heapify( int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort( int arr[], int n)
{
for ( int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for ( int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; ++i)
cout << arr[i] << " " ;
cout <<endl;
}
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = 10;
heapSort(arr, n);
cout << "array after using heap sort:" <<endl;
printArray(arr, n);
}
|
Outputarray after using heap sort:
0 1 2 3 4 5 6 7 8 9
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
insertion sort code:
C++
#include <bits/stdc++.h>
using namespace std;
void insertionSort( int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray( int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " " ;
cout << endl;
}
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int N =10;
insertionSort(arr, N);
cout<< "array after using insertion sort:" <<endl;
printArray(arr, N);
return 0;
}
|
Outputarray after using insertion sort:
0 1 2 3 4 5 6 7 8 9
Time Complexity: O(N^2)
Auxiliary Space: O(1)