Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

quicksort java

import java.util.Random;

public class main{
public static void main(String[] args) {
    Random rand=new Random();
    int[]numbers=new int[60000000];
    for (int i = 0; i < numbers.length; i++) {
        numbers[i]=rand.nextInt(10000);
    }
    System.out.println("before");
    quickSort(numbers);
    System.out.println("after");
   // printArray(numbers);
    
}
static void printArray(int []numbers){
    for(int x:numbers){
        System.out.println(x);
    }
}
static void quickSort(int[]numbers){
    quickSort(numbers,0,numbers.length-1);
}
static void quickSort(int[]numbers,int lowIndex,int heighIndex){
if(lowIndex>=heighIndex)return;
int pivotIndex=new Random().nextInt(heighIndex-lowIndex)+lowIndex;
int pivot=numbers[pivotIndex];
int leftPointer=lowIndex;
int righPointer=heighIndex;
swap(numbers, pivotIndex, heighIndex);
while(leftPointer<righPointer){
    while(numbers[leftPointer]<=pivot&&leftPointer<righPointer){
        leftPointer++;
    }
    while(numbers[righPointer]>=pivot&&leftPointer<righPointer){
        righPointer--;
    }
    swap(numbers,leftPointer, righPointer);
}
swap(numbers, leftPointer,heighIndex);
quickSort(numbers,lowIndex,leftPointer-1);
quickSort(numbers, leftPointer+1, heighIndex);

}
static void swap(int[]numbers,int index1,int index2){
int  temp=numbers[index1];
numbers[index1]=numbers[index2];
numbers[index2]=temp;
}
    

 }   
Source by rosettacode.org #
 
PREVIOUS NEXT
Tagged: #quicksort #java
ADD COMMENT
Topic
Name
7+5 =