Verification: a143cc29221c9be0

Php array sort by index

Содержание

C++

#include

using namespace std;

void partSort(int arr[], int N, int a, int b)

{

    int l = min(a, b);

    int r = max(a, b);

    int temp[r - l + 1];

    int j = 0;

    for (int i = l; i

        temp[j] = arr[i];

        j++;

    }

    sort(temp, temp + r - l + 1);

    j = 0;

    for (int i = l; i

            arr[i] = temp[j];

            j++;

    }

    for (int i = 0; i

            cout " " ;

        }

}

int main()

{

    int arr[] = { 7, 8, 4, 5, 2 } ;

    int a = 1 ;

    int b = 4;

    int N = sizeof(arr) / sizeof(arr[0]);

    partSort(arr, N, a, b);

    return 0;

}

Java

import java.io.*;

import java.util.*;

import java.lang.*;

class GFG {

    static void partSort(int[] arr, int N, int a, int b)

    {

        int l = Math.min(a, b);

        int r = Math.max(a, b);

        int[] temp = new int[r - l + 1];

        int j = 0;

        for (int i = l; i

            temp[j] = arr[i];

            j++;

        }

        Arrays.sort(temp);

        j = 0;

        for (int i = l; i

            arr[i] = temp[j];

            j++;

        }

        for (int i = 0; i

            System.out.print(arr[i] + " ");

        }

    }

    public static void main(String args[])

    {

        int[] arr = { 7, 8, 4, 5, 2 };

        int a = 1, b = 4;

        int N = arr.length;

        partSort(arr, N, a, b);

    }

}

Python3

def partSort(arr, N, a, b):

    l = min(a, b)

    r = max(a, b)

    temp = [0 for i in range(r - l + 1)]

    j = 0

    for i in range(l, r + 1, 1):

        temp[j] = arr[i]

        j += 1

    temp.sort(reverse = False)

    j = 0

    for i in range(l, r + 1, 1):

            arr[i] = temp[j]

            j += 1

    for i in range(0, N, 1):

            print(arr[i], end = " ")

if __name__ == '__main__':

    arr = [7, 8, 4, 5, 2]

    a = 1

    b = 4

    N = len(arr)

    partSort(arr, N, a, b)

C#

using System;

class GFG {

    static void partSort(int[] arr, int N, int a, int b)

    {

        int l = Math.Min(a, b);

        int r = Math.Max(a, b);

        int[] temp = new int[r - l + 1];

        int j = 0;

        for (int i = l; i

            temp[j] = arr[i];

            j++;

        }

        Array.Sort(temp);

        j = 0;

        for (int i = l; i

            arr[i] = temp[j];

            j++;

        }

        for (int i = 0; i

            Console.Write(arr[i] + " ");

        }

    }

    public static void Main()

    {

        int[] arr = { 7, 8, 4, 5, 2 };

        int a = 1, b = 4;

        int N = arr.Length;

        partSort(arr, N, a, b);

    }

}

PHP

# PHP program to sort the

# array in a given index range

function partSort( $arr, $N, $a, $b)

{

    $l = min($a, $b);

     $r = max($a, $b);

    $temp = array();

    $j = 0;

    for ($i = $l; $i $r; $i++) {

        $temp[$j] = $arr[$i];

        $j++;

    }

    sort($temp);

    $j = 0;

    for ($i = $l; $i $r; $i++) {

            $arr[$i] = $temp[$j];

            $j++;

    }

    for ($i = 0; $i $N; $i++) {

            echo $arr[$i]." " ;

        }

}

    $arr = array( 7, 8, 4, 5, 2 ) ;

    $a = 1 ;

    $b = 4;

    $N = count($arr);

    partSort($arr, $N, $a, $b);

    ?>

Javascript

    function partSort(arr, N, a, b)

    {

        let l = Math.min(a, b);

        let r = Math.max(a, b);

        let temp = new Array(r - l + 1);

        temp.fill(0);

        let j = 0;

        for (let i = l; i

            temp[j] = arr[i];

            j++;

        }

        temp.sort(function(a, b){return a - b});

        j = 0;

        for (let i = l; i

            arr[i] = temp[j];

            j++;

        }

        for (let i = 0; i

            document.write(arr[i] + " ");

        }

    }

    let arr = [ 7, 8, 4, 5, 2 ];

    let a = 1, b = 4;

    let N = arr.length;

    partSort(arr, N, a, b);

C++

#include

using namespace std;

    void partSort(int arr[], int N, int a, int b)

    {

        int l = min(a, b);

        int r = max(a, b);

        vectorint> v(arr, arr + N);

        sort(v.begin() + l, v.begin() + r + 1);

        for (int i = 0; i

            cout " ";

    }

    int main()

    {

        int arr[] = { 7, 8, 4, 5, 2 };

        int a = 1, b = 4;

        int N = sizeof(arr)/sizeof(arr[0]);

        partSort(arr, N, a, b);

    }

Java

import java.io.*;

import java.util.*;

import java.lang.*;

class GFG {

    static void partSort(int[] arr, int N, int a, int b)

    {

        int l = Math.min(a, b);

        int r = Math.max(a, b);

        Arrays.sort(arr, l, r + 1);

        for (int i = 0; i

            System.out.print(arr[i] + " ");

    }

    public static void main(String args[])

    {

        int[] arr = { 7, 8, 4, 5, 2 };

        int a = 1, b = 4;

        int N = arr.length;

        partSort(arr, N, a, b);

    }

}

Python3

def partSort(arr, N, a, b):

    l = min(a, b)

    r = max(a, b)

    arr = (arr[0 : l] +

    sorted(arr[l : r + 1]) +

           arr[r : N])

    for i in range(0, N, 1):

            print(arr[i], end = " ")

if __name__ == '__main__':

    arr = [ 7, 8, 4, 5, 2 ]

    a = 1

    b = 4

    N = len(arr)

    partSort(arr, N, a, b)