This document defines a quicksort algorithm for sorting an integer array. It includes methods for sorting the entire array, sorting partitions of the array, and swapping elements. The main method creates a sample array, calls the sorting method, and prints the sorted results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
31 views3 pages
Java - Util.arrays Quick (: Import Public Class
This document defines a quicksort algorithm for sorting an integer array. It includes methods for sorting the entire array, sorting partitions of the array, and swapping elements. The main method creates a sample array, calls the sorting method, and prints the sorted results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
import java.util.
Arrays;
public class quick {
public void sort(int[] array)
{ sort(array, 0, array.length - 1); }
private void sort(int[] array, int start, int end)
{ if (start < end) { // select pivot element (left-most) int pivot = array[start]; // partition and shuffle around pivot int i = start; int j = end; while (i < j) { // move right to avoid pivot element i += 1; // scan right: find elements greater than pivot while (i <= end && array[i] < pivot) { i += 1; } // scan left: find elements smaller than pivot while (j >= start && array[j] > pivot) { j - = 1; } if (i <= end && i < j) { // swap around pivot swap(array, i, j); } } // put pivot in correct place swap(array, start, j); // sort partitions sort(array, start, j - 1); sort(array, j + 1, end); } } private void swap(int[] array, int i, int j) { if (i >= 0 && j >= 0 && i < array.length && j < array.length) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; } }