Last active
December 12, 2021 17:43
-
-
Save sunmeat/2d1dfd910c459c4c226b to your computer and use it in GitHub Desktop.
quick sort example (recursive)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alex.recursion; | |
public class QuickSortExample { | |
static void quickSort(int ar[], int start, int end) { | |
int L = start, R = end; | |
int M = ar[(start + end) / 2]; | |
do { | |
while (ar[L] < M) { | |
L++; | |
} | |
while (ar[R] > M) { | |
R--; | |
} | |
if (L <= R) { | |
int temp = ar[L]; | |
ar[L] = ar[R]; | |
ar[R] = temp; | |
L++; | |
R--; | |
} | |
} while (L <= R); | |
if (start < R) { | |
quickSort(ar, start, R); | |
} | |
if (L < end) { | |
quickSort(ar, L, end); | |
} | |
} | |
public static void main(String[] args) { | |
int size = 10; | |
int ar[] = new int[size]; | |
// до сортировки | |
for (int i = 0; i < size; i++) { | |
ar[i] = (int) (Math.random() * 100); | |
System.out.print(ar[i] + " "); | |
} | |
System.out.print("\n\n"); | |
quickSort(ar, 0, ar.length - 1); | |
// после сортировки | |
for (int i = 0; i < size; i++) { | |
System.out.print(ar[i] + " "); | |
} | |
System.out.print("\n\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment