Sort the array using slow sort

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 12Input: 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++
// C++ program for the above approach#include <iostream>using namespace std;Â
// Function to swap two elementsvoid swap(int* xp, int* yp){Â Â Â Â int temp = *xp;Â Â Â Â *xp = *yp;Â Â Â Â *yp = temp;}Â
// Function to sort the array using// the Slow sortvoid slow_sort(int A[], int i, int j){    // Recursion break condition    if (i >= j)        return;Â
    // Store the middle value    int m = (i + j) / 2;Â
    // Recursively call with the    // left half    slow_sort(A, i, m);Â
    // Recursively call with the    // right half    slow_sort(A, m + 1, j);Â
    // Swap if the first element is    // lower than second    if (A[j] < A[m]) {        swap(&A[j], &A[m]);    }Â
    // Recursively call with the    // array excluding the maximum    // element    slow_sort(A, i, j - 1);}Â
// Function to print the arrayvoid printArray(int arr[], int size){Â Â Â Â int i;Â Â Â Â for (i = 0; i < size; i++)Â Â Â Â Â Â Â Â cout << arr[i] << " ";Â Â Â Â cout << endl;}Â
// Driver Codeint main(){    // Given Input    int arr[] = { 6, 8, 9, 4, 12, 1 };    int n = sizeof(arr) / sizeof(arr[0]);Â
    // Function Call    slow_sort(arr, 0, n - 1);Â
    // Print the sorted array    printArray(arr, n);Â
    return 0;} |
Java
// Java program for the above approachclass GFG{Â
// Function to sort the array using// the Slow sortstatic void slow_sort(int A[], int i, int j){         // Recursion break condition    if (i >= j)        return;Â
    // Store the middle value    int m = (i + j) / 2;Â
    // Recursively call with the    // left half    slow_sort(A, i, m);Â
    // Recursively call with the    // right half    slow_sort(A, m + 1, j);Â
    // Swap if the first element is    // lower than second    if (A[j] < A[m])     {        int temp = A[j];        A[j] = A[m];        A[m] = temp;    }Â
    // Recursively call with the    // array excluding the maximum    // element    slow_sort(A, i, j - 1);}Â
// Function to print the arraystatic void printArray(int arr[], int size){Â Â Â Â int i;Â Â Â Â for(i = 0; i < size; i++)Â Â Â Â Â Â Â Â System.out.print(arr[i] + " ");Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println();}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int arr[] = { 6, 8, 9, 4, 12, 1 };Â Â Â Â int n = arr.length;Â
    // Function Call    slow_sort(arr, 0, n - 1);Â
    // Print the sorted array    printArray(arr, n);}}Â
// This code is contributed by abhinavjain194 |
Python3
# Python3 program for the above approachÂ
# Function to sort the array using# the Slow sortdef slow_sort(A, i, j):         # Recursion break condition    if (i >= j):        return             # Store the middle value    m = (i + j) // 2         # Recursively call with the    # left half    slow_sort(A, i, m)Â
    # Recursively call with the    # right half    slow_sort(A, m + 1, j)Â
    # Swap if the first element is    # lower than second    if (A[j] < A[m]):        temp = A[m]        A[m] = A[j]        A[j] = tempÂ
    # Recursively call with the    # array excluding the maximum    # element    slow_sort(A, i, j - 1)Â
# Function to print the arraydef printArray(arr, size):Â Â Â Â Â Â Â Â Â for i in range(size):Â Â Â Â Â Â Â Â print(arr[i], end = " ")Â
# Driver Codeif __name__ == '__main__':         arr = [ 6, 8, 9, 4, 12, 1 ]    n = len(arr)         # Function Call    slow_sort(arr, 0, n - 1)         # Print the sorted array    printArray(arr, n)Â
# This code is contributed by SoumikMondal |
C#
// C# implementation of the approach using System;Â
class GFG {   // Function to sort the array using// the Slow sortstatic void slow_sort(int[] A, int i, int j){         // Recursion break condition    if (i >= j)        return;Â
    // Store the middle value    int m = (i + j) / 2;Â
    // Recursively call with the    // left half    slow_sort(A, i, m);Â
    // Recursively call with the    // right half    slow_sort(A, m + 1, j);Â
    // Swap if the first element is    // lower than second    if (A[j] < A[m])     {        int temp = A[j];        A[j] = A[m];        A[m] = temp;    }Â
    // Recursively call with the    // array excluding the maximum    // element    slow_sort(A, i, j - 1);}Â
// Function to print the arraystatic void printArray(int[] arr, int size){Â Â Â Â int i;Â Â Â Â for(i = 0; i < size; i++)Â Â Â Â Â Â Â Â Console.Write(arr[i] + " ");Â Â Â Â Â Â Â Â Â Â Â Â Â Console.WriteLine();}Â
    // Driver code    public static void Main()     {    int[] arr = { 6, 8, 9, 4, 12, 1 };    int n = arr.Length;Â
    // Function Call    slow_sort(arr, 0, n - 1);Â
    // Print the sorted array    printArray(arr, n);    }}Â
// this code is contributed by sanjoy_62. |
Javascript
<script>Â
    // JavaScript program for the above approach         // Function to sort the array using    // the Slow sort    function slow_sort(A, i, j)    {Â
        // Recursion break condition        if (i >= j)            return;Â
        // Store the middle value        let m = parseInt((i + j) / 2, 10);Â
        // Recursively call with the        // left half        slow_sort(A, i, m);Â
        // Recursively call with the        // right half        slow_sort(A, m + 1, j);Â
        // Swap if the first element is        // lower than second        if (A[j] < A[m])        {            let temp = A[j];            A[j] = A[m];            A[m] = temp;        }Â
        // Recursively call with the        // array excluding the maximum        // element        slow_sort(A, i, j - 1);    }Â
    // Function to print the array    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;      // Function Call    slow_sort(arr, 0, n - 1);      // Print the sorted array    printArray(arr, n);     </script> |
1 4 6 8 9 12
Â
Best Case Time Complexity:Â , where e > 0
Average Case Time Complexity:Â
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



