Summary of some sorting algorithms

Source: Internet
Author: User

1. Insert sorting directly

Public class ChaRu {/* Insert sort directly. If there are already several records in an ordered sequence, the sorted elements are compared with the new elements in sequence. Locate the appropriate location and insert */public static void main (String args []) {int [] numbers = {,}; System. out. println ("----- Initial Sequence -----"); for (int m: numbers) System. out. println (m); int j, k; int n = numbers. length; for (j = 1; j <n; j ++) {int temp = numbers [j]; k = J-1; while (k> = 0 & temp <numbers [k]) {numbers [k + 1] = numbers [k]; k --;} numbers [k + 1] = temp;} System. out. println ("----- order after sorting -----"); for (int s: numbers) System. out. println (s );}}

2. Select sort

Public class ZhiJieXuanZe {/* directly select the sort order to find the small I elements one by one and place them to the I position of the array */public static void main (String args []) {int numbers [] = {, 32}; int n = numbers. length; for (int j = 0; j <n; j ++) {int smallest = j; // select the smallest subscript of the remaining elements for (int k = j + 1; k <n; k ++) if (numbers [k] <numbers [smallest]) {smallest = k;} int temp = numbers [j]; numbers [j] = numbers [smallest]; numbers [smallest] = temp;} for (int s: numbers) system. out. println (s );}}

}

3. Bubble Sorting

Public class MaoPao {/* the bubble sort method constantly compares adjacent elements. If the sorting requirement is not met, the adjacent elements are exchanged until all the elements are sorted. */Public static void main (String args []) {int [] numbers = {,}; int n = numbers. length; for (int j = 0; j <n-1; j ++) {// n-1; // each trip compares Adjacent Elements successively, place the maximum number at the end of the array for (int k = 0; k <n-j-1; k ++) if (numbers [k]> numbers [k + 1]) {int temp = numbers [k]; numbers [k] = numbers [k + 1]; numbers [k + 1] = temp ;}} for (int s: numbers) system. out. println (s );}}

}

4. Quick sorting

Public class KuaiSu2 {/* fast sorting divides elements into several groups. First, select an axis and gradually approach the elements at the (0 + length-1)/2] position, replace the number greater than the axis value on the left with the number less than the axis value on the right to the left. */Public static void FenGe (int left, int right, int [] numbers) {int l = left; int r = right; int location = (l + r)/2; int mid = numbers [location]; System. out. println ("Call segmentation"); while (l <= r) {while (numbers [l] <mid & l <right) l ++; while (numbers [r]> mid & r> left) r --; if (l <= r) {int temp = numbers [l]; numbers [l] = numbers [r]; numbers [r] = temp; l ++; r -- ;}} if (l <right) FenGe (l, right, numbers); if (r> left) FenGe (left, r, numbers);} public static void main (String args []) {int numbers [] = {12, 34, 56, 78, 32, 23, 214,54, 33,21}; // 32 is the axis int n = numbers. length; FenGe (0, n-1, numbers); for (int s: numbers) System. out. println (s );}}

}

This article from the "Qingyun flow" blog, please be sure to keep this source http://wxhhbdx.blog.51cto.com/2986356/1304521

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.