(i) bubble sort, select sort, insert sort

Source: Internet
Author: User
Tags array length benchmark data structures sort

Recently looking at data structure and algorithm analysis, want to write a note to record, first from the basic bubble, select, insert Start.

Note: This is illustrated by the example of the increment sequence

First, bubble sort

1, principle: Starting from the first position of the array 22 compare Array[index] and array[index+1], if ARRAY[INDEX] greater than array[index+1] then Exchange Array[index] and array[index+1] Position, ending to the end of the array;

Starting from the first position of the array, repeat the above action, ending with an array length minus one position;

Starting from the first position of the array, repeat the above action, ending with an array length minus two positions;

。。。。

2. Time complexity: O (N²), carried out (n-1) * (n-2) .... =n* (n-1)/2 comparisons and the number of exchanges in half (in all cases), then the time complexity is O (n^2) According to the large O notation

3. Code

/**
* Bubbling algorithm for adding sequences
* @param array
*/
public static void Bubblingsort (int[] array) {
for (int i=0;i<array.length;i++) {
for (int j=0;j<array.length-i-1;j++) {
if (Array[j]>array[j+1]) {
Swap (array,j,j+1);
}
}
}
}

private static void Swap (int[] array,int x,int y) {
int temp = array[x];
ARRAY[X] = Array[y];
Array[y] = temp;
}


Second, choose the sort

1, principle: Select a value array[0] as a benchmark, and then loop to find the smallest value except this value (find the minimum value less than the benchmark), exchange these two values, then the minimum value is placed on the array[0], and then the array[1] as a benchmark, from the remaining unsorted value found in the minimum value, And swap the two values.

Figure: (Figure in data structure and algorithm)

2. Time complexity: O (n^2), reducing the number of array exchanges compared to bubble sort

3. Code

/**
* Select sort Add Order
* @param array
*/
public static void Selectsort (int [] array) {
for (int i=0;i<array.length;i++) {
int index = i;
for (int j=i+1;j<array.length;j++) {
if (Array[j] < Array[index]) {
index = j;
}
}
if (i! = index) swap (I,index,array);
Display (array);
System.out.println ();
}
}


public static void swap (int a,int b,int array[]) {
int temp = Array[a];
Array[a] = array[b];
ARRAY[B] = temp;
}

Third, insert sort

1. Principle: The idea of inserting a sort is that the array is ordered by the department, and then the unordered part loops are inserted into the ordered sequence.

Figure: (picked from data structures and algorithms)

2. Time complexity: The time complexity of the sequence in which the order is inserted is also O (n^2), but the order time complexity for the basic ordered sequence is O (N)

3. Code

/**
* Insert Sort Ascending
* @param array
*/
public static void Insertsort (int [] array) {
for (int out=1;out<array.length;out++) {
int temp = array[out];//The marked value or the value that is currently required to be inserted
int in = out;
Move backward if the round-robin value is greater than the marked value
while (in > 0 && temp < array[in-1]) {
Array[in] = array[in-1];
In--;
}
Inserts the tagged value into the empty position that was eventually moved out
Array[in] = temp;
}
}


Next write: Hill sort, quick sort


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.