Java Sort Basics

Source: Internet
Author: User

1. Select Sort:

Idea: Select sort (Descending) each round of the loop finds the largest number of remaining numbers in the first place of the current number of rounds, that is, the maximum value is chosen;

such as the first round i = 0 o'clock: The first wheel of Nums[0], and then from Nums[1] to start comparing, each found larger than Nums[0] number is exchanged data, until the end of the round. The next round takes nums[1] as the first, starting from nums[2], until the end.

Sort Ascending instead.

Select Sort {3,2,4,1} (Descending)
I J 1 2 3
0 {3,2,4,1} {4,2,3,1} —
1 {4,3,2,1} — —
2 — — —
3 — — —

Package Com.planz;public class Selectsort {public static void main (string[] args) {int[] nums1 = by (New int[]{3,2,4,1} , "desc"); for (int i = 0; i < nums1.length; i++) {System.out.print (nums1[i]+ "");} System.out.println (); int[] Nums2 = (new int[]{3,2,4,1}, "Esc"); for (int i = 0; i < nums2.length; i++) {System.ou T.print (nums2[i]+ "");}} public static int[] [int[] nums,string type) {if (Type.equalsignorecase ("desc")) {for (int i = 0; i < nums.length ; i++) {for (int j = i+1; J < Nums.length; J + +) {if (nums[i]<nums[j]) {int tem = nums[i];nums[i] = nums[j];nums[j] = t Em;}}}}  else if (type.equalsignorecase ("ESC")) {for (int i = 0; i < nums.length; i++) {for (int j = i+1; J < Nums.length; J + +) {if (nums[i]>nums[j]) {int tem = nums[i];nums[i] = nums[j];nums[j] = tem;}}}} return nums;}}
2. Bubble Sort:

Idea: (1) bubble sort divided into two data fields, an unordered area, each time need to traverse the comparison, each round descending, an orderly area, each round increment, at the end of the array;

(2) Bubble sort is sorted by default in ascending order, the small number floats like a bubble (left), and the large number sinks like a stone (right shift);

(3) The outer loop number is n-1, and the number of inner layer traversal is disordered area;

Bubble sort {3,2,4,1} (Ascending)
I J 0 1 2
0 {2,3,4,1} {2,3,4,1} {2,3,1,4}
1 {2,3,1,4} {2,1,3,4}
2 {1,2,3,4}

Package Com.planz;public class Bubblesort {public static void main (string[] args) {int[] nums = by (new int[]{3,2,4,1}) ; for (int i = 0; i < nums.length; i++) {System.out.print (nums[i]+ "");}}  public static int[] Order (int[] nums) {for (int i = 0; i < nums.length-1; i++) {//Cycle theory number (n-1) cycles can be for (int j = 0; J < Nums.length-1-i; J + +) {//unordered-area traversal, remaining sorted ordered area if (Nums[j]>nums[j+1]) {//small number floating int tem = NUMS[J];NUMS[J] = nums[j+1];nums[j+1] = tem;}}} return nums;}}

In addition there are quick sorting, direct insertion of sorting, dichotomy, using Array.Sort (int[] array), etc., and so on, and so on, hehe ....

Java Sort Basics

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.