Sort the array using slow sort
Last Updated :
22 Jun, 2021
Given an array arr[] consisting of N integers, the task is to sort the given array in ascending order using the slow sort.
Examples:
Input: arr[] = {6, 8, 9, 4, 12, 1}
Output: 1 4 6 8 9 12
Input: arr[] = {5, 4, 3, 2, 1}
Output: 1 2 3 4 5
Approach: Like Merge Sort, Slow Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself the two halves, and then compares the maximum element of the two halves. It stores the maximum element of a sub-array at the top position of the sub-array, then, it recursively calls the sub-array without the maximum element. Follow the steps below to solve the problem:
SlowSort(arr[], l, r):
- If r >= l, perform the following steps:
- Find the middle value of the array as m = (l + r) / 2.
- Recursively call function SlowSort to find the maximum of first half elements: SlowSort(arr, l, m)
- Recursively call function SlowSort to find the maximum of second-half elements: SlowSort(arr, m + 1, r)
- Store the largest of two maxima returned from the above function calls at the end as arr[r] = max(arr[m], arr[r])
- Recursively call function SlowSort without the maximum obtained in the above step: SlowSort(arr, l, r-1)
The following figure shows the complete Slow Sort process. For example, array {9, 6, 8, 4, 1, 3, 7, 2}. From the figure, it can be observed that the array is recursively divided into two halves till the size becomes 1. Once the size becomes 1, the comparison process begins.
Slow Sort
Below is the implementation for the above approach:
C++
#include <iostream>
using namespace std;
void swap( int * xp, int * yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void slow_sort( int A[], int i, int j)
{
if (i >= j)
return ;
int m = (i + j) / 2;
slow_sort(A, i, m);
slow_sort(A, m + 1, j);
if (A[j] < A[m]) {
swap(&A[j], &A[m]);
}
slow_sort(A, i, j - 1);
}
void printArray( int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " " ;
cout << endl;
}
int main()
{
int arr[] = { 6, 8, 9, 4, 12, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
slow_sort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
|
Java
class GFG{
static void slow_sort( int A[], int i, int j)
{
if (i >= j)
return ;
int m = (i + j) / 2 ;
slow_sort(A, i, m);
slow_sort(A, m + 1 , j);
if (A[j] < A[m])
{
int temp = A[j];
A[j] = A[m];
A[m] = temp;
}
slow_sort(A, i, j - 1 );
}
static void printArray( int arr[], int size)
{
int i;
for (i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
System.out.println();
}
public static void main(String[] args)
{
int arr[] = { 6 , 8 , 9 , 4 , 12 , 1 };
int n = arr.length;
slow_sort(arr, 0 , n - 1 );
printArray(arr, n);
}
}
|
Python3
def slow_sort(A, i, j):
if (i > = j):
return
m = (i + j) / / 2
slow_sort(A, i, m)
slow_sort(A, m + 1 , j)
if (A[j] < A[m]):
temp = A[m]
A[m] = A[j]
A[j] = temp
slow_sort(A, i, j - 1 )
def printArray(arr, size):
for i in range (size):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 6 , 8 , 9 , 4 , 12 , 1 ]
n = len (arr)
slow_sort(arr, 0 , n - 1 )
printArray(arr, n)
|
C#
using System;
class GFG
{
static void slow_sort( int [] A, int i, int j)
{
if (i >= j)
return ;
int m = (i + j) / 2;
slow_sort(A, i, m);
slow_sort(A, m + 1, j);
if (A[j] < A[m])
{
int temp = A[j];
A[j] = A[m];
A[m] = temp;
}
slow_sort(A, i, j - 1);
}
static void printArray( int [] arr, int size)
{
int i;
for (i = 0; i < size; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
}
public static void Main()
{
int [] arr = { 6, 8, 9, 4, 12, 1 };
int n = arr.Length;
slow_sort(arr, 0, n - 1);
printArray(arr, n);
}
}
|
Javascript
<script>
function slow_sort(A, i, j)
{
if (i >= j)
return ;
let m = parseInt((i + j) / 2, 10);
slow_sort(A, i, m);
slow_sort(A, m + 1, j);
if (A[j] < A[m])
{
let temp = A[j];
A[j] = A[m];
A[m] = temp;
}
slow_sort(A, i, j - 1);
}
function printArray(arr, size)
{
let i;
for (i = 0; i < size; i++)
document.write(arr[i] + " " );
document.write( "</br>" );
}
let arr = [ 6, 8, 9, 4, 12, 1 ];
let n = arr.length;
slow_sort(arr, 0, n - 1);
printArray(arr, n);
</script>
|
Best Case Time Complexity: , where e > 0
Average Case Time Complexity:
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...