Related articles
Algorithm (i) Time complexity
Preface
Sorting is the basis of the algorithm, there are many ways to sort it out, some methods are simple to implement, but inefficient, we can call these sorts of methods an elementary sort. In this article we will learn the insertion sort and bubble sort in elementary sort.
1. Insert Sort
Insert sort it's easy to think of the idea and the order of arranging cards when playing poker is similar. For example, we take cards in the left hand, and then use the right hand to the cards from left to right, from small to large to sort, this requires us to arrange the cards to be drawn out to the appropriate position, and continue to repeat, until the order of the cards, the process can be understood as the insertion sort.
Plot Insert sort
An array that needs to be sorted is divided into two parts during the insert sort process: sorted and unsorted, as shown in.
It can be seen that the array is divided into two parts, where the subscript 0, 1, 2 elements are arranged, and the remainder is the non-aligned part.
Insert Collation:
Treats the beginning element as the sort part. The following processing is performed until there is no unsorted part.
-Remove the beginning element of the unsorted section to assign to the variable V that temporarily holds the data.
-Moves all elements that are larger than V to one position in the arranged section.
-Insert the removed element V into the vacancy.
In accordance with this rule, let's give a simple example. We sort the array a={8,3,1,5,2,1} from small to large, as shown in arrays a.
We sort the array a by a total of 5 steps:
1. Then we see a[0]=8 as sorted, we start with a[1], take the value of a[1] 3 out, 3 is less than the value of a[0] 8, so move the value of a[0] 8 to a[1], and then insert 3 into a[0], as shown in.
2.A[2] The value of 1 is smaller than the value of a[0] and a[1], then the a[0] and a[1] are moved backwards by one position, then 1 is inserted into the a[0], as shown in.
3. Take the 5 out of a[3], which is larger than the a[2] 8, so 8 move backwards, insert 5 into a[3]. As shown in.
4. Take a[4] 2 out and find that the values in a[1], a[2], a[3] are greater than 2, so move them backwards, then insert 2 into a[1], as shown in.
5. Finally move the 1 in a[5] to the appropriate position, as in the above, and the final sort results as shown.
implementing an Insert sort
The next step is to implement the insert sort, which defines the variables for.
As shown, I represents the beginning element of the unsorted part, V is the variable that temporarily holds the a[i] value, and J represents the position where the sorted part V is to be inserted.
Based on the three variables defined, the implementation of the insertion Order is: The outer loop i increment from 1, and the value of A[i] is saved in V at the beginning of each loop, and the inner loop is J starting from i-1 and moving the element greater than V from A[j] to a[j+1] and inserting v into the current j+ The position of 1 (after the inner loop, J will subtract 1 first, so the place of insertion is the position of the j+1), when J equals-1 or a[j] is less than or equal to v the inner loop ends.
Next we use the code to implement the insertion sort, as shown below.
Public classInsertsort { Public Static void Main(string[] args) {intA[] = {8,3,1,5,2,1}; Arrayutils.printarray (a);intB[] = insert (a); Arrayutils.printarray (b); } Public Static int[]Insert(int[] a) {intI, J, V;intn = a.length; for(i =1; I < n; i++) {v = a[i]; j = i-1; while(J >=0&& A[j] > V) {a[j +1] = A[j]; j--; } A[j +1] = V; }returnA }}
The Arrayutils class, which is responsible for printing the array, is shown below.
Public classarrayutils { Public Static void PrintArray(int[] array) {System. out. Print ("{");intLen=array.length; for(inti =0; i < Len; i++) {System. out. Print (Array[i]);if(I < Len-1) {System. out. Print (", "); }} System. out. println ("}"); }}
The output is:
{8, 3, 1, 5, 2, 1}
{1, 1, 2, 3, 5, 8}
the complexity of inserting sorting
Based on the complexity of the algorithm (a), let's calculate the time complexity of the insertion sort. In the worst case, each I loop needs to perform the I move, which requires a total of 1+2+......+N-1=N2/2+N/2, according to the previous derivation of the rules of the large O-order, we derive the time complexity of the Insertion Order O (N2).
2. Bubble sort
Bubble sort should be the easiest sort algorithm for developers to understand, the basic idea is to compare two adjacent elements each time, if they are in the wrong order to swap them over. The elements that need to be sorted move toward the water as slowly as the bubbles in it.
Plot bubble sort
As with the insert sort, the array that needs to bubble sort is also divided into sorted and unsorted parts.
the rules for bubbling sorting are: starting from the end of the array, compare the adjacent two elements, and if the size relationship is reversed, swap positions until there are no more adjacent elements in the array that are in reverse order.
We sort the array a={5,3,2,4,1} from small to large, and the sort process is as follows.
First round sort:
We compare the value of a[4] at the end of the array to the value of a[3], and find that the value of a[4] is smaller than the value of a[3], then they are exchanged, then the remaining adjacent two elements are compared and exchanged, the result is a={1,5,3,2,4}, and the ordered portion of the element is 1.
Second round sort:
First compare the values of a[3] and a[4], and find that the value of a[3] is smaller than the value of a[4], then you do not need to sort. The resulting result is a={1,2,5,3,4}, and the sorted portion of the element is 1, 2.
Third round sort:
After the third round of sorting, the elements of the sorted part are 1, 2, 3.
Fourth round sort:
After four rounds of sorting we end up with the result of a={1,2,3,4,5}
Implementing Bubble Sorting
To implement an insert sort, we define two variables, I is a loop variable, which represents the beginning of the unsorted part, and moves from the beginning of the array to the end. J is also a loop variable, used to compare adjacent elements 22 in the unsorted part, starting at the end of the array n-1 to the end of I (I=1).
The code implementation is shown below.
Public classBubblesort { Public Static void Main(string[] args) {intA[] = {5,3,2,4,1}; Arrayutils.printarray (a);intB[] = Bubble (a); Arrayutils.printarray (b); } Public Static int[]Bubble(int[] a) {intI, J, V;intn = a.length; for(i =1; I <= N-1; i++) { for(j = n-1; J >= I; j--) {if(A[j] < a[j-1]) {v = a[j]; A[J] = a[j-1]; A[j-1] = V; } } }returnA }}
One of the Arrayutils's PrintArray methods has been mentioned before, here is no longer given, printing results are:
{5, 3, 2, 4, 1}
{1, 2, 3, 4, 5}
the complexity of the bubbling sort
In the worst case, the bubbling sort is performed on the unsorted part of the adjacent element (n-1) + (n-2) + (n-3) +......+1 times comparison, that is, N2/2+N/2 times comparison, according to the derivation of the rules of the large O-order we derive the time complexity of the bubble sort to O (N2).
GitHub source
Please pay attention to my public number, the first time to get blog update reminders, as well as more into the system of Android-related technology dry.
Sweep the QR code below to follow:
Algorithm (ii) Elementary Sort prev [insert and bubble sort]