- 2022-01-17 21:39
*views 4*- algorithm
- Quick sort
- data structure
- Java

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

- Java244 blogs
- Python241 blogs
- Vue112 blogs
- algorithm96 blogs
- c language94 blogs
- Flow Chart81 blogs
- MySQL71 blogs
- C++64 blogs
- more...

Daily Recommendation

© ioDraw All rights reserved

js of indexOf method computer network - Agreement and hierarchy Computer function key name , Do you know the basic knowledge of computer keyboard function Deep understanding of processes and threads Computer keyboard letter memory , keyboard 26 What is a letter formula ? stay uniapp Used in element-ui assembly PTA— Read numbers （C language ） Two methods QFontJAVA realization QQ Sign in , Registration and other functions number IC Hand tear code （ seven ）