Sorting is a problem that we often face in our lives. Students in the exercise will be in accordance from the low to high arrangement, the teacher to check the attendance in class, will be in accordance with the student number sequence name, the college entrance examination, will be in descending order according to the total score of the admission. Sorting is an important operation used in data processing, which plays a very important role in our program development.

The sorting is divided into the following four categories in a total of seven sorting methods:

Exchange Sort:

① Bubble Sort

② Quick Sort

Select Sort:

③ Direct Selection Sort

④ Heap Sorting

Insert Sort:

⑤ Direct Insert Sort

⑥ Hill Sort

Merge sort:

⑦ Merge Sort

This article mainly summarizes the exchange of sorts (that is, bubble sort and quick sort), the basic idea of exchange sorting is: 22 Compare the elements to be sorted, if you find two elements in reverse order, then exchange, until all the elements are not reversed. I will summarize from the following aspects:

1. Bubble Sorting and algorithm implementation

2. Fast sequencing and algorithm implementation

3. Bubble Sort vs Quick sort

1. Bubble Sorting and algorithm implementation

What is bubble sort? Bubble sort is a simple sort method, its basic idea is: through the comparison and exchange between the adjacent two elements, so that the larger elements gradually moved from the front to the back (ascending), like the bubbles in the water gradually bubbling upward, so called "bubble" sort. The worst time complexity for bubbling sorting is O (N2), and the average time complexity is O (n2)

The entire process of bubbling sorting is shown below, where the square brackets are the next round of elements to be sorted, and the first element after the square brackets is the largest element in the order of this round.

1-1.

1-2. Code

The code implementation of the Bubbling sorting algorithm:

Bubblesort.java

Public classBubblesort { Public Static voidMain (string[] args) {int[] list = {36, 28, 45, 13, 67, 37, 18, 56}; System.out.println ("************ Bubble sort ************"); System.out.println ("Before sorting:"); Display (list); System.out.println ("After sorting:"); Bubblesort (list); Display (list); } /*** Traverse Print*/ Public Static voidDisplayint[] list) {System.out.println ("******** Show begins ********"); if(List! =NULL&& list.length > 0) { for(intnum:list) {System.out.print (num+ " "); } System.out.println (""); } System.out.println ("******** Show ends ********"); } /*** Bubble Sorting algorithm*/ Public Static voidBubblesort (int[] list) { inttemp; //How many rounds to sort (up to length-1 rounds) for(inti = 0; i < list.length-1; i++) { //how many are compared in each round? for(intj = 0; J < list.length-1-I; J + +) { if(List[j] > list[j + 1]) { //Exchange Ordertemp =List[j]; LIST[J]= List[j + 1]; List[j+ 1] =temp; } } } }}

Test results:

2. Fast sequencing and algorithm implementation

Quick sort is an improved method for bubble sort, in which the comparison and exchange of elements are carried out between adjacent elements, and the elements can only move one position at a time, so the number of comparisons and the number of moves is relatively low. In the quick sort, the element comparison and exchange is from the two ends to the middle, the larger elements can be exchanged to the back of the position, and the smaller elements can be exchanged to the front position, the element each move is far away, so the number of comparisons and moves less, faster, so called "fast sorting."

The basic idea of fast sorting is to divide the ordered elements into separate two parts by a round order, in which all the elements are smaller than all the elements in the other part, and then the elements of the two parts are ordered in a fast order, so that the whole sequence becomes an ordered sequence. The worst time complexity for fast sorting is O (N2), and the average time complexity is O (n*log2n)

2-1.

2-2. Code

Code implementation of the fast sorting algorithm:

Quicksort.java

Public classQuickSort { Public Static voidMain (string[] args) {int[] List = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8}; System.out.println ("************ Quick Sort ************"); System.out.println ("Before sorting:"); Display (list); System.out.println ("After sorting:"); QuickSort (list,0, List.length-1); Display (list); } /*** Fast sorting algorithm*/ Public Static voidQuickSort (int[] list,intLeftintRight ) { if(Left <Right ) { //split Array, find split point intPoint =partition (list, left, right); //recursive invocation, quick ordering of left dial hand arraysQuickSort (list, left, point-1); //recursive invocation, quick ordering of right sub-arraysQuickSort (list, point + 1, right); } } /*** Split Array, find split point*/ Public Static intPartitionint[] list,intLeftintRight ) { //use the first element of an array as a base number intFirst =List[left]; while(Left <Right ) { while(Left < right && List[right] >=First ) { Right--; } //Exchangeswap (list, left, right); while(Left < right && List[left] <=First ) { Left++; } //Exchangeswap (list, left, right); } //returns the location where the split point is located returnLeft ; } /*** Swap elements of two positions in an array*/ Public Static voidSwapint[] list,intLeftintRight ) { inttemp; if(List! =NULL&& list.length > 0) {Temp=List[left]; List[left]=List[right]; List[right]=temp; } } /*** Traverse Print*/ Public Static voidDisplayint[] list) {System.out.println ("******** Show begins ********"); if(List! =NULL&& list.length > 0) { for(intnum:list) {System.out.print (num+ " "); } System.out.println (""); } System.out.println ("******** Show ends ********"); }}

Test results:

3. Bubble Sort vs Quick sort

The code is as follows:

Bubblevsquick.java

Public classBubblevsquick { Public Static voidMain (string[] args) {Testquick (); Testbubble (); } /*** Test the time spent on quick sequencing*/ Public Static voidTestquick () {int[] List =New int[10000]; for(inti = 0; I < 10000; i++) {List[i]= (int) (Math.random () * 100000); } //Quick Sort LongStart =System.currenttimemillis (); Quicksort.quicksort (list,0, List.length-1); LongEnd =System.currenttimemillis (); System.out.println ("Time spent in quick sorting:" + (End-start)); Display (list); } /*** Test the time spent in bubble sequencing*/ Public Static voidtestbubble () {int[] List =New int[10000]; for(inti = 0; I < 10000; i++) {List[i]= (int) (Math.random () * 100000); } //Bubble Sort LongStart =System.currenttimemillis (); Bubblesort.bubblesort (list); LongEnd =System.currenttimemillis (); System.out.println ("Bubble sort Time Spent:" + (End-start)); Display (list); } /*** Traverse the first 10 digits of printing*/ Public Static voidDisplayint[] list) {System.out.println ("******** The first 10 numbers after sorting start********"); if(List! =NULL&& list.length > 0) { for(inti = 0; I < 10; i++) {System.out.print (List[i]+ " "); } System.out.println (""); } System.out.println ("******** The first 10 numbers after sorting end**********"); System.out.println (""); }}

Test results:

As you can see, fast sorting is faster than bubbling sorting.

Welcome reprint, but please keep the original source of the article

This address: http://www.cnblogs.com/nnngu/p/8281030.html

Algorithm 1 sorting algorithm: Bubble sort and quick sort