Java Common algorithms (bubbling, selecting, inserting, quick-row)

Source: Internet
Author: User

1. The implementation of bubble sort

Bubbling sorting is the most basic and simplest of all sorting algorithms. The idea is to exchange the sort, by comparing and exchanging adjacent data to achieve the purpose of sorting.

The length of the set array is n:

1, compared with the adjacent two data, if the previous data is greater than the subsequent data, the two data exchange.

2, so that the No. 0 data of the array to N-1 data after one traversal, the largest data is "sink" to the N-1 position of the array.

3, N=n-1, if N is not 0, repeat the previous two steps, otherwise the sorting is complete.

The code can be written quickly by this definition:

public static void sort (int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
for (int j = 1; j < N-i; J + +) {
if (Arr[j-1] > Arr[j]) {
int temp = arr[j-1];
ARR[J-1] = Arr[j];
ARR[J] = temp;
}
}
}
}

2. Select the sorting implementation select the sorting algorithm to reorder by selecting the minimum value in each step, and sorting by selection and exchange.  The process is as follows: 1. First select the smallest 1 data from the original array and place it in the first position.  2, then select one of the smallest data from the remaining data and place it in the second position of the original array. 3, continue to repeat the selection and exchange process, until the final sorting is complete.

public static void sort (int[] arr) {
int temp;
int small;
for (int i = 0; i < arr.length; i++) {
small = i;
Find the subscript for the minimum number
for (int j = i+1; J < Arr.length; J + +) {
if (Arr[j] < Arr[small]) {
small = j;
}
}
Exchange
if (small! = i) {
temp = Arr[i];
Arr[i] = Arr[small];
Arr[small] = temp;
}
}
}

3. Insert sort implementation direct insertion sorting is the insertion of unsorted data into the appropriate position of the ordered sequence.  The process is as follows: 1, first compare the array of the first two data, and sort, 2, compare the third element with the first two rows of data, and put the third element in the appropriate position, 3, compare the fourth element with the first three ordered data, and put the fourth element into the appropriate position; 4, until the last element into the appropriate position. public static void sort (int[] arr) {

int temp;
for (int i = 1; i < arr.length; i++) {
Data to be inserted
temp = Arr[i];
Int J;
for (j = i-1; J >= 0; j--) {
To determine if it is greater than temp, or greater then move back
if (Arr[j] > Temp) {
ARR[J+1] = Arr[j];
}else{
Break
}
}
ARR[J+1] = temp;
}
}

4. Implementation of FAST Sequencing

Fast sorting, as the name implies, is a fast, efficient sorting algorithm.

Fast-Track principle: In the number of rows to be selected (such as array a) to select a central value key (such as a[0]), through a sort of array a divided into two parts, with key as the center, the key to the right of the key is larger than keys, key to the left of the key is small, and then repeat the process for the two        The whole process of the fast-running is simplified in order to go through the sequencing process, and then the recursive call is done. A trip to the sorting method: 1, define i=0,j=a.lenght-1,i as the first number of subscript, J for the last number subscript 2, from the last number of the array AJ from right to left, find the first number less than key, recorded as Aj;3, from the first number of arrays Ai from left to right to find, Find the first number greater than key, remember as ai;4, swap AI and AJ 5, repeat the process until i=j6, adjust key position, A[i] and key exchange

public static void Sort (Int. arr[],int Low,int High) {
1. Find the exit of the program
if (Low > High) {
Return
}
2. Save
int i = low;
int j = high;
3. Key
int key = Arr[i];
4. Complete a comparison
while (I < j) {
4.1 Find a number less than key from the right
while (I<j && arr[j] > key) {
j--;
}
4.2 Find a number greater than key from the left
while (I<j && arr[i] <= key) {
i++;
}
4.3 Exchange
if (i<j) {
int p = Arr[i];
Arr[i] = Arr[j];
ARR[J] = p;
}
}
4.4 Jump Key position
int p = Arr[i];
Arr[i] = Arr[low];
Arr[low] = p;
5. Quickly sort data to the left of key
Sort (arr,low,i-1);
6. Quickly sort data to the right of key
Sort (Arr,i+1,high);
}

Java Common algorithms (bubbling, selecting, inserting, quick-row)

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.