Bubble Select Sort

Source: Internet
Author: User

The algorithm is always the basis of programming, and the sorting algorithm is the beginning of the learning algorithm, and the sorting is the important content of data processing. A sort is an ordered sequence that organizes an unordered sequence into a non-descending order. There are many ways to arrange, depending on the size of the sequence to be sorted and the processing requirements of the data, different sorting methods can be used. Then tidy up the online search data, according to their own understanding, the C language of the 8 ranking algorithm listed.

In the general sense, the sorting algorithm can be divided into three main categories:

1 Exchange Class sorting method
2 Insert Class Sort method
3 Select Class Sort method

I. Exchange-Class sorting method

The so-called Exchange sorting method refers to the method of sorting by exchanging data elements with each other. The bubble sort and quick sort method all belong to the Exchange class sorting method.

1. Bubble sort (bubblesort)

Basic concepts of bubbling sorting:

Compare adjacent two numbers in turn, place decimals ahead, and large numbers behind. That is, in the first trip: first compare the 1th and 2nd numbers, put the decimals before the large number. Then compare the 2nd and 3rd numbers, place the decimal before the large number, and then continue until you compare the last two numbers, put the decimals before the decimal, and put the large number behind. At the end of the first trip, the largest number was put to the last. In the second pass: The comparison is still starting from the first logarithm (because the 1th number is no longer less than the 2nd number due to the 2nd and 3rd number of interchanges), before the decimal, the large number is placed, it is compared to the second-to-last number (which is already the largest in the penultimate position), the second trip ends, Get a new maximum number in the penultimate position (actually the second largest number in the whole sequence). So go on, repeat the above process until the final sort is finished. Since the decimal is always placed forward in the sorting process, the large number is placed backwards, which is the equivalent of bubbles rising, so called bubble sort.

Realize:

The outer loop variable is set to I and the inner loop variable is set to J. If there are 10 numbers that need to be sorted, the outer loop repeats 9 times, and the inner loop repeats 9,8,...,1 times in turn. Each of the two elements of the comparison is related to the inner loop j, they can be identified by a[j] and a[j+1], the value of I sequentially,..., 9, for each of the values of the i,j in turn,... 10-i.

Icon:

C Language implementation:

1voidBublesort (intA[],intN
2{
3intI,j,k;
4 for(j=0; j<n;j++)/*Bubble method to sort n times*/
5{
6 for(i=0; i<n-j;i++)/*when the value of a larger element sinks, only the maximum value of the remaining elements can be sunk again.*/
7{
8if(a[i]>a[i+1])/*Sink the value of a larger element to the end*/
9{
TenK=a[i];
Onea[i]=a[i+1];
Aa[i+1]=k;
-}
-}
the}
-}

Performance Analysis:

If the initial state of the record sequence is "positive order", then the bubbling sort process only needs to be sorted by a single stroke, and the n-1 is not moved, and the record is not shifted, but if the initial state of the record sequence is "reverse", then N (n-1)/2 comparisons and record movement are required. So the total time complexity of the bubble sort is O (n*n).

Blog Park, there is an article on the Bubble algorithm optimization, you can look at the two optimizations:

One of the Vernacular classic algorithm series three implementations of bubbling sorting

2. Quick Sort (Quicksort)

Basic idea:

A quick sort is an improvement to the bubbling sort. by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

implementation:Set to sort the array is a[0] ... A[n-1], the first arbitrary selection of data (usually the first data) as the key data, and then put all the smaller than its number in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm. A quick sorting algorithm is: 1) Set two variables I, J, the beginning of the order: i=0,j=n-1; 2) with the first array element as the key data, assign a value to Key, i.e. Key=A[0]; 3) forward search from J, that is, after the start of a forward search (J--), find the first less than Key(A[j],a[i] and a[j] exchange; 4) backward search from I, i.e. backward search from before (i + +), find the first one greater than KeyA[i],a[i] and A[j], 5) Repeat steps 3rd, 4, and 5 until i=j, (3,4 step is not found in the program when J=j-1,i=i+1, until found. When I find and exchange I, the position of the J pointer does not change. Also when i=j this process must be exactly the end of the final loop of i+ or J completion. )diagram:To illustrate:

such as unordered array [6 2 4 1 5 9]

a)remove the first item [6] First,

Use [6] to compare the rest of the items in turn,

If the [6] is smaller than [6], 2 4 1 5 are smaller than [6], so all put to the front of [6]

If compared to [6] large put [6] behind, 9 than [6] large, put to [6] behind,//6 out after a shout, than my small station front, bigger than my station behind, action!

After a trip, it becomes the following:

Sort before 6 2 4 1 5 9

After sort 2 4 1 5 6 9

b)proceed to the Ban La [2 4 1 5] for quick sequencing

Repeat step a) to change to the following:

Sort before 2 4 1 5

After sort 1 2 4 5

Before the ban La Sort is complete, the total sort is done as well:

Before sorting: [6 2 4 1 5 9]

After sorting: [1 2 4 5 6 9]

C language implementation: 1intPartitionint*data,intLowintHigh
2
3{
4intt =0;
5
6t = Data[low];
7
8 while(Low < High)
9
Ten{ while(Low < High && Data[high] >= t)
One
Ahigh--;
-
-Data[low] = Data[high];
the
- while(Low < High && Data[low] <= t)
-
-low++;
+
-Data[high] = Data[low];
+
A}
at
-Data[low] = t;
-
-returnLow
-
-}
in
- voidSortint*data,intLowintHigh//the pivot of each trip is to be re-determined,//One-step determination of the low and high values for each small record to be discharged
to
+{if(Low >= High)
-
thereturn;
*
$intPivotloc =0;
Panax Notoginseng
-Pivotloc = partition (Data,low,high);
the
+Sort (data,low,pivotloc-1);
A
theSort (data,pivotloc+1, high);
+
-}


Performance analysis

The time of fast sorting is mainly spent on the division operation, the interval of length k is divided, the total need k-1 the comparison of the key words.

The worst-case scenario is that each time the selected datum is the smallest (or largest) record of the keyword in the current unordered region, the result is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and the number of records in another non-empty sub-interval is divided by only one less than the number of records Time Complexity of O (n*n)

In the best case, the datum for each partition is the "median" record of the current unordered region, and the result is that the length of the two unordered sub-ranges of the datum is roughly equal to the left and right. Total number of keyword comparisons: O (NLGN)

Although the worst time for fast sorting is O (N2), in terms of average performance, it is the fastest in the internal sorting algorithm based on the keyword comparison, and hence the name of the fast sort. Its average time complexity is O (NLGN).

Here's a video that illustrates these two interesting sorting algorithms visually: http://www.tudou.com/programs/view/htKY1-Rj9ZE/?resourceId=0_06_02_99

Bubble Select 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.