Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Programming with JAVA object-oriented method
Programming with JAVA object-oriented method
Quick sorting of public classes {

Private int[] data;

Quick sort (int [] data) (

this.data = data

}

Public void quick sort () {

recQuickSort(data,0,data . length- 1);

}

Private void recQuickSort(int[] data, int low, int high) {

//Set two sliders

int low cursor = low+ 1;

Int highCursor = high;

//Temporary variables during exchange

int temp = 0;

//Compare the perspective values and set them as the first value of the array.

int medi = data[low];

while (true) {

//Search from the low end and determine the position of data[low] that is greater than the number.

while(low cursor & lt; High & & data [low cursor] < medi) {

low cursor++;

}

//Search from the high end and determine the position [low end] smaller than the data. Use > = to judge that it is less than this value.

while(high cursor & gt; Low & amp& ampdata[high cursor]& gt;; = medi) {

high cursor-;

}

//The positions of the two cursors are out of bounds and exit the loop.

if(low cursor & gt; = highCursor) {

Break;

}

//Exchange position data of data [highCursor] and data [lowCursor].

temp = data[high cursor];

data[high cursor]= data[low cursor];

data[low cursor]= temp;

}

//according to the while loop exit condition: lowCursor > highCursor.

//The current lowCursor points to the first position on the right that is greater than data[low];

//and the highCursor points to the first position on the left that is less than data[low], so you need to exchange data[low] and.

//Value of data [high cursor]

Data [low] = data [high cursor];

data[high cursor]= medi;

//Recursively operate the left half.

If (low & lthighCursor) {

RecQuickSort (data, low cursor, high cursor);

}

//Recursively operate the right half.

if(low cursor & lt; High) {

RecQuickSort (data, low cursor, high);

}

}

//Print array

Public void display () {

for(int I = 0; I < data length; i++) {

system . out . print(data[I]+" ");

}

system . out . println();

}

//Generate an array containing large and small random numbers.

public static int[]get data(int size){

int data[]= new int[size];

for(int I = 0; I < data length; i++) {

data[I]=(int)( 1+math . random()* 54); //Generate a random number 1-54, where Math.random () is the number that generates 0- 1

}

Return data;

}

//test

Public static void main(String[] args) {

int data[]= get data( 10);

QuickSort sort = new QuickSort (data);

sort.display()。

sort . quick sort();

sort.display()。

}

}