Java Algorithm sorting-Bubble sorting and quick sorting

Source: Internet
Author: User

Basic Idea of bubble sort (1): In the number of a group to be sorted, the number of all numbers in the range not sorted yet, from top to bottom, compare and adjust the adjacent two numbers in sequence, so that a large number will sink and a small number will go up. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged. (2) Example: (3) using java to implement [plain] publi cclass bubbleSort {public bubbleSort () {int a [] = }; int temp = 0; for (int I = 0; I <. length; I ++) {for (int j = I + 1; j <. length; j ++) {if (a [I]> a [j]) {temp = a [I]; a [I] = a [j]; a [j] = temp ;}}for (int I = 0; I <. length; I ++) System. out. println (a [I]) ;}after a reminder from the Taoist friends, I found that the above is not an authentic Bubble sorting, So I corrected it: Authentic Bubble Sorting: [plain] public class bubbleSort {public bubbleSort () {int a [] = {49,38, 65,97, ,}; Int temp = 0; for (int I = 0; I <. length-1; I ++) {for (int j = 0; j <. length-1-i; j ++) {if (a [j]> a [j + 1]) {temp = a [j]; a [j] = a [j + 1]; a [j + 1] = temp ;}}for (int I = 0; I <. length; I ++) System. out. println (a [I]) ;}} but what is the first sort? It's a bit similar to sorting, but it's not. You can just look at the top of your fingers and try again and again! 6. quick sorting (1) Basic Idea: select a benchmark element. Generally, select the first or last element and scan the column to be sorted into two parts, some of them are smaller than the base element, and some of them are greater than or equal to the base element. At this time, the base element is in the correct position after its sorting order, and then the two parts are recursively sorted and divided using the same method. (2) Example: (3) Implement [plain] public class quickSort {inta [] = {, 54, 56,17, 18,23, 34,15, 35,25, 53,51}; public quickSort () {quick (a); for (int I = 0; I <. length; I ++) System. out. println (a [I]);} publicint getMiddle (int [] list, int low, int high) {int tmp = list [low]; // The first of the array is used as the axis while (low

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.