Guruh talabasi Abduvohid Ergashev 9-variant QuickSort 4200 elementli massiv


Download 43.9 Kb.
Sana23.11.2020
Hajmi43.9 Kb.
#150802
Bog'liq
quicksort


314-18 guruh talabasi Abduvohid Ergashev

9-variant QuickSort 4200 elementli massiv

Dastur kodi:

package saralash;

import java.util.Random;

public class quickMain {


public static void main(String[] args) {
quickSort quickSort = new quickSort();
Random rand = new Random();
int [] arr = new int[4200];
for(int i=0; i<4200; i++){
arr[i] = rand.nextInt(1000);
}
System.out.println("Tasodifiy sonlardan tanlangan massiv : ");
printArray(arr);
quickSort.sort(arr, 0, arr.length-1);
System.out.println("Saralangan massiv: ");
printArray(arr);
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; iSystem.out.print(arr[i]+" ");
System.out.println();
}
}

package saralash;

public class quickSort {
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

int temp = arr[i + 1];


arr[i + 1] = arr[high];
arr[high] = temp;

return i + 1;


}

void sort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}
}

Dastur natijasi:




Download 43.9 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling