Quick sorting method :
* Gu mingsiyi , Quick sort method is a quick sort algorithm in practice , stay c++ Or right java Basic types are particularly useful in sorting . Its average running time is 0(N log
N). The reason why the algorithm is particularly fast , Mainly due to the very concise and highly optimized internal circulation .
*
Quick sorting is an improvement of bubbling method . The data to be sorted is divided into two independent parts by one sorting , All the data in one part is smaller than all the data in the other part , Then quickly sort the two parts of data according to this method , The whole sorting process can be carried out recursively , In this way, the whole data becomes an ordered sequence .
Sketch Map :
here Define the leftmost element by left The rightmost element is right
p Value of element namely 2 Corresponding index 0 add 5 Corresponding index 7 Sum of divide 2 obtain Index is 3 Corresponding element 7
Use left greater than 7 The number on the right is less than 7 Number of change of position Keep going also middle p Always change your position
until Finish arranging
code implementation :
import java.util.Arrays; public class kuaisu { public static void
main(String[] args) { int arrays[]=new int[]{2,9,4,7,3,3,6,5};
sort(arrays,0,arrays.length-1); System.out.println(Arrays.toString(arrays)); }
public static void sort(int arrays[],int left,int right){ int l=left ;// Given subscript
int r=right;// Given subscript int temp; // Define a variable As intermediate value exchange Element positions on the left and right int
pivot=arrays[(left+right)/2];// Intermediate value while(l<r){ // Find elements on the left that are less than the middle value
while(arrays[l]<arrays[pivot]){ l++; } // Similarly, look for elements larger than the middle value on the right
while(arrays[r]>arrays[pivot]){ r--; }// It ends when the left element is larger than the right element if(l>=r){ break; }
temp=arrays[l]; arrays[l]=arrays[r]; arrays[r]=temp; // End of exchange arrays[l]=pivot
if(arrays[l]==pivot){ r--; } if(arrays[r]==pivot){ l++; } if(r==l){ // To make the left element
Move to the left Move right element to the right stagger l++; r--; } // Recursion on the left if(left<r){ sort(arrays,left,r);
}// Recursion on the right if(right>l){ sort(arrays,l,right); } } } }
<>
Console output results as follows :
I hope the big guys can do what they don't write enough Help me pay I will work harder !
A glowing little white !
Nothing is as good as friends' one button three times. Ha ha ha !
Technology