Swap sort (bubble sort, quick sort)--java

Source: Internet
Author: User

Exchange sort

(1) Bubble sort

1, the basic idea: in order to sort a group of numbers, the current is not yet ranked in the range of all the number, top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller to the top. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.

2. Example

  3. Java implementation

public static void Main (string[] args) {int []nums={57,68,59,52};bubblesort1 (nums);} /** * It iterates through the sequence of columns to be sorted, compares two elements at a time, and if their order is wrong, exchange them. * The work of traversing the sequence is repeated until there is no more need to exchange * that is, the sequence is already sorted * This algorithm is named because the larger element will slowly "float" through the exchange. To the front of the series * Bubble sorting features: * If only one or two of the data in your list is out of order, use this algorithm is the fastest sorting algorithm * If the data in your data list is random, then this method becomes the slowest algorithm */public static void  Bubblesort1 (int[] nums) {int len=nums.length;int temp=0;for (int i = len-1; I >0; i--) {for (int j = 0; J < i; J + +) {if (Nums[j]>nums[j+1]) {temp=nums[j];nums[j]=nums[j+1];nums[j+1]=temp;}}}}

4. Analysis

Bubble sort is a stable sort method.

If the first form of the file is a positive sequence, then a trip to bubble can be completed sorting, sorting code compared to n-1, and no record movement, time complexity is O (n) If the initial state of the file is in reverse order, it is necessary to n-1 the bubble, each trip to n-i the comparison of the order code, and each comparison moved three times, Maximum number of comparisons and moves: O (N2) bubble sort Average Time complexity O (n2)
(2) Quick Sort

Quick Layout Diagram1, the basic idea set to sort the array is a[0] ... A[n-1], the first arbitrary selection of data (usually the first number of the array) as the key data, and then all the smaller than the number of it in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm. A quick sorting algorithm is: 1) Set two variables I, J, the beginning of the order: I=0,J=N-1;2) with the first array element as the key data, assigned to key, i.e., key=a[0];3) from the start of J forward search, that is, after the start of the forward search (j--), Find the first value less than key A[j], will a[j] and A[i] interchange, 4) from I start backward search, that is, to start backward search (i++), to find the first greater than the key a[i], the a[i] and A[j] interchange, 5) repeat 3rd, 4 steps until i=j; (3,4 step, Did not find the qualifying value, that is, 3 a[j] is not less than the key,4 A[i] is not larger than the time to change the value of J, I, so that j=j-1,i=i+1 until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).2. Example3. Java Implementation
Import java.util.*;p ublic class quicksort{/** its basic idea is to divide the sorted data into two separate parts by a single trip, One part of the data is smaller than any other part of the data, and then the two parts of the data are quickly sorted by this method the entire sorting process can be recursive, in order to achieve the entire data into an ordered sequence. */public static void Main (string[] args) {int[] Nums={57,68,59,52,72,28,96,33,24,19};quick (nums); System.out.println (arrays.tostring (Nums));} public static void Quick (int[] nums) {if (nums.length>0) {quickSort1 (nums,0,nums.length-1);}} public static void QuickSort1 (Int[]nums,int low,int High) {if (low

4. Analysis

Quick Sort is an unstable sort.

The time complexity for quick sorting is O (Nlogn).

When n is larger, it is better to use fast parallelism, but it is not good to use fast platoon when the sequence is basically ordered.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swap sort (bubble sort, quick sort)--java

Related Article

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.