Eight Sorting Algorithm JAVA Implementation (time complexity O (n * n), eight java

Source: Internet
Author: User

Eight Sorting Algorithm JAVA Implementation (time complexity O (n * n), eight java

This article mainly describes three sorting algorithms with a time complexity of n2: Bubble sorting, select sorting, and insert sorting.

 

1. Bubble Sorting:Starting from the array header, two elements are compared at a time. If their order is incorrect, they are exchanged. After each exchange, the maximum value of the current array is placed at the end.

1 public int [] bubbleSort (int [] a, int n) 2 {3 for (int j = 0; j <n-2; j ++) 4 {5 for (int I = 0; I <n-j-1; I ++) 6 {// if the current number is greater than the next one, otherwise, continue to look at the next number 7 if (a [I]> a [I + 1]) 8 {9 int temp = a [I + 1]; 10 a [I + 1] = a [I]; 11 a [I] = temp; 12} 13} 14} 15 return a; 16} 17

Input parameter: a is the array to be sorted, and n is the length of the array.

In the first for loop, use the j value to control the second loop, that is, compare the length of the array. According to the definition of Bubble sorting, the maximum value is placed at the end of each time, so the next row can have less than one number;

In the second for loop, compare the two numbers and place the big ones behind them.

The first for loop in this question is a small optimization that can be used without directly exchanging the entire array.

 

2. Select sorting:Select the smallest number in the array and place it in the corresponding position of the array header.

1 public int [] selectionSort (int [] a, int n) 2 {3 for (int j = 0; j <n-1; j ++) 4 {// j indicates that you need to search for a maximum value in the array starting with j to fill the j-bit 5 int maxi = 0; 6 for (int I = 0; I <n-j; I ++) 7 {// maxi indicates the subscript 8 if (a [maxi] <a [I]) 9 {10 maxi = I; 11} 12} 13 if (maxi! = N-j-1) 14 {15 int temp = a [n-j-1]; 16 a [n-j-1] = a [maxi]; 17 a [maxi] = temp; 18} 19} 20 return a; 21}

3. Insert sorting:Simulate it to insert a value into an ordered array. The key is to move the numbers in an ordered array one by one later than the current number.

1 public int [] insertionSort (int [] a, int n) 2 {3 for (int I = 1; I <n; I ++) 4 {// assume that a [0] has only one number and is ordered. Therefore, starting from the second number, insert 5 int nowNum = a [I]; 6 int j = I-1; 7 for (; j> = 0; j --) 8 {// move all the numbers larger than the inserted Number Backward. After the loop ends, a blank space will be set up. 9 if (nowNum <a [j]) 10 {11 a [j + 1] = a [j]; 12} 13 else14 {15 break; 16} 17} 18 a [++ j] = nowNum; 19} 20 return a; 21}

 

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.