Basic sequencing of data structures and algorithms

Source: Internet
Author: User
Tags rounds

There are eight types of classic sorting algorithms, namely:

? Bubble sort

? Select sort

? Insert Sort

? Merge sort

? Hill sort

? Quick Sort

? Heap Sort

? Base sort

Where bubble sort, select sort, insert sort are called three basic sorts.

Although the time complexity of the three basic sorting algorithms are O (N2), in fact, under discussion, there are also their own characteristics.

Basic ideas:

Let's say we need to sort in ascending order

The N-wheel comparison, each round will be adjacent to the two elements of the next comparison, according to the size of the exchange, after each round of comparison, the largest element in turn ' take ' to the end of the array, after a few rounds of comparison, the array will show an orderly state.

Graphic:

Suppose you make a pseudo-sort of 5 elements, first prepare 5 random elements:

  

1) in the first round, the first element (10) is compared to the adjacent element (20) because the 20:10 is large so there is no need to swap. The second element (20) is compared with the adjacent element (5) because it is 20:5 large, so it needs to be ' taken ', so it needs to be exchanged with 5.

  

The third element (20) is then compared with the adjacent element so that it is taken to the last position after comparing with 14 and 1, because the 20 is the largest.

  

2) Each subsequent round is compared in this way, but it is important to note that each subsequent round needs to be compared less than the previous one, since the position of the largest element has already been determined, and in order to improve performance, it is no longer possible to compare the elements that determine the position behind.

3) It can be determined that, when the length of the array is n, the number of wheels to compare is N-1 wheel. Each round from the beginning of the first element of the adjacent element 22 to compare, the first round needs to compare N-1 times, the 2nd round need to compare N-2 times, and so on, to achieve the overall sort

  

? First, the number of wheels to compare is determined by an outer for loop, and the number of circular elements-1 rounds.

? The effect of the 22-element comparison of each round is achieved internally through a for loop. The number of times each round needs to be compared is less than the previous one, and is achieved through jarray.length-1-i.

? Element 22 comparison is implemented by Array[j] array[j+1].

The above bubble sorting method also has the space to optimize. Because if an array is already fully ordered, the bubble sort method will still perform a round-by-wheel comparison, which is undoubtedly a waste of performance behavior. So we can be sure that, when the bubble comparison, there is a round if no swap occurs, you can be sure that the current array is fully ordered, the number of rounds behind is completely unnecessary. So make the following adjustments:

  

By defining an identifier Issort, if there is no exchange in one round, then Issort will be true, the next time a direct break, the number of rounds will not be compared.

Basic ideas:

Select sort and bubble sort differently, select sort by a round comparison, choose the lowest element of the subscript, and then exchange with the first element, so that the position of the first element can be determined.

The second round of the same, starting from the second element to compare, select the smallest element of the subscript, and the second element exchange position, so that the second small element position is determined.

followed by and so on until the entire array is rendered in an orderly state.

  

? The number of wheels to select the sort comparison is the same as the bubble sort comparison

? K represents the current minimum value of the subscript, is currently the first round, K first represents the subscript of the element, and then followed by the elements to compare, if found to be smaller than the K-position element, change the subscript of K, so that the position of K after the round is the smallest element of the round, and I exchange can

? If the k==i after a round, then I would be the smallest element, without switching to improve performance.

Basic ideas

Assuming an array is basically ordered, then inserting a sort is a good choice. Insert ordering is to ensure that the left element is basically ordered, and then the following elements are compared to the left element, if compared to a smaller than their own elements can stop the comparison, because the left is already an orderly state, find smaller than their own elements, there is no further comparison.

  

? The number of rounds to be inserted is the same as the bubbling sort, but I start with 1 because we assume that the first element is already in an orderly state.

? The inner loop starts from the current position, compares it to the previous element, and if it finds a smaller element than itself, stops the comparison and exits the comparison directly, making the next round comparison

? Although the time complexity of the three sorting methods is O (N2), there are different performance differences in different scenarios.

? The bubble sorting method is the worst-performing algorithm, and in the formal application, the bubble sort method is not used to sort.

? Selecting a sort minimizes the number of interchanges, but the number of comparisons is still large. Choosing a sort is a good choice when the amount of data is small and the exchange time is more time-consuming than the comparison.

? In the case of basic order, the insertion sort is the best choice, but if the data is basically reversed, the performance of the insertion sort and the bubbling sort are basically the same.

? In the average case, the insertion sort performance is slightly better than the selection sort.

?

Basic sequencing of data structures and algorithms

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.