Size of the array:
Speed of the algorithm:
void merge(int *Arr, int start, int mid, int end) { int temp[end - start + 1]; int i = start, j = mid+1, k = 0; while(i <= mid && j <= end) { if(Arr[i] <= Arr[j]) { temp[k++] = Arr[i++]; } else { temp[k++] = Arr[j++]; } } while(i <= mid) { temp[k++] = Arr[i++]; } while(j <= end) { temp[k++] = Arr[j++]; } for(i = start; i <= end; i += 1) { Arr[i] = temp[i - start] } } void mergeSort(int *Arr, int start, int end) { if(start < end) { int mid = (start + end) / 2; mergeSort(Arr, start, mid); mergeSort(Arr, mid+1, end); merge(Arr, start, mid, end); } }
int partition (int arr[], int low, int high){
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++){
if (arr[j] <= pivot){
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}
void quicksort(int a[], int p, int r) {
if(p < r){
int q;
q = partition(a, p, r);
quicksort(a, p, q-1);
quicksort(a, q+1, r);
}
}
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 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 selectionSort(int arr[], int n){
int i, j, min_idx;
for (i = 0; i < n-1; i++){
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
void bubbleSort(int arr[], int n){
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(arr[j], arr[j+1]);
}