bubble sort = = = Quick Sort

Source: Internet
Author: User

C Quick Sort1). Description1. Pick an element from the series, called the "benchmark"(pivot) 2. Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the datum (the same number can be on either side). After the partition exits, the datum is in the middle of the sequence. This is called partition (partition) operation. 3recursively (recursive) sorts sub-columns that are smaller than the base value elements and sub-columns that are larger than the base value elements.2). Sample CodeThe sample code is C, the input parameter, the array that needs to be sorted is arr[], takes out an element as pivot, and according to pivot value, divides the array into two regions, which are larger than pivot and less than pivot, and recursively completes the sort.3). Copy the Code1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void Quick_sort (char *arr, int begin, intEnd) 5{6 char pivot =Arr[begin]; 7 intI,j; 8 i =Begin 9 J =End;10 while (I <J) {One while (Arr[j] >= pivot && i <J) J--; Arr[i] =Arr[j];14 while (Arr[i] <= pivot && i <j) i + +; Arr[j] = arr[i];17 }18 arr[i] =  pivot;19 if (i-1;  begin) Quick_sort (arr, begin, I-1 ); F (End > i + 1 ) Quick_sort (arr, i + 1 , end), + }25 int  main () +  {char ch[] = "qwe Rtyuiopasdfghjklzxcvbnm "; quick_sort (CH, 0, + ); printf ("%s\n ", ch); exit (0 ); } Output ABCDEFGHIJKLMNOPQRSTUVWXYZ 
I. Algorithm analysis Worst time complexity Θ (n2) Optimal time complexity θ (nlog N) Average time complexity θ (nlog N) two. Principle known as a set of unordered data a[1], a[2] 、...... a[n], you need to sort them in ascending order.
First take data a[x] as the benchmark.
Compare A[x] with other data and sort, so that a[x] is ranked at the K-bit of the data, and each data in A[1]~a[k-1] <a[x],a[k+1]~a[n] >a[x],
then uses Span style= "font-size:18pt" > Divide the policy respectively to a[1]~a[k-1] and a[k+1]~ A[n] Two sets of data for quick sorting. three. Pros and cons advantages: Very fast, less data movement;
Cons: Unstable.

two. Bubble sort 1) algorithm descriptionBubble sort (Bubble sort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order.
The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.
The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence. The bubble sort requires a comparison of O (N2) for n items, and can be sorted in situ (in-place).
Although this algorithm is one of the simplest to understand and implement a sorting algorithm, it is inefficient for ordering a sequence of numbers other than a few elements. The bubbling sort is the same execution time as the insertion sort, but the two methods differ greatly in the number of exchanges required. In the worst case scenario, the bubble sort requires an O (N2) Exchange, while the insertion sort is as long as the O (N) interchange.

The naïve bubbling sort implementation (similar to the following) usually performs poorly on the sequenced sequence (O (N2)), whereas the insertion order in this example requires only O (n) operations. As a result, many modern algorithmic textbooks avoid using bubble sorting and replace them with insertion sorting. Bubble sort if the internal loop is executed for the first time, it is possible to use a flag to indicate that there is no need to swap, and it is possible to reduce the best complexity to O (n). In this case, there is no need to exchange the sequence numbers that have been queued. If the sequence of visits and the size of comparison are reversed, the efficiency can be improved slightly.2) The Bubbling Sorting algorithm works as follows: Compares adjacent elements. If the first one is bigger than the second one, swap them both. 1The same work for each pair of adjacent elements, starting from the first pair to the end. At this point, the last element should be the maximum number. 2Repeat the above steps for all elements except the last one. 3Continue to repeat the above steps each time for fewer elements until there are no pairs of numbers to compare.3) Sample code1 #include <stdlib.h> 2 #include <stdio.h> 3 4 void Bubble_sort (int a[], const INTSize) 5{6 int flag = 0; 7 int temp = 0; 8 intI 9 for (i = 0; i < size-1; i + +)) {Ten flag = 1; intJ;12 for (j = 0; J < size-i-1; j + +)) {if (A[j] > a[j + 1]) {+ temp =a[j];15 A[j] = a[j + 1];16 a[j + 1] = temp;17 flag = 0 ; }19 }20 if (flag) break ; }23 }24 int Main () + {number[int] = {5,3,8,4,1,7,9,2,0,6 };28 bubble_sort (number, sizeof (Int_array)/sizeof (int )) ; int i;30 for (i = 0; i < sizeof (Int_array)/sizeof (int); i + + ) to printf ("%d\n" , Number[i]); 32 } Copy code output 0123456789 one. Principle Known a set of unordered data a[1], a[2] 、...... A[n], You need to sort them in ascending order.

First compares the values of a[1] and a[2], and if A[1] is greater than a[2, the values of both are exchanged, otherwise they are unchanged.
Compare the values of a[2] and a[3], and if a[2] is greater than a[3, the values of both are exchanged, otherwise they will not change.
Compare a[3] with a[4], and so on, and finally compare the values of a[n-1] and A[n].

After this round of processing, the value of a[n] must be the largest in this set of data.

to a[1]~a[n-1] in the same way, the value of a[n-1] must be the largest of a[1]~a[n-1]. Again to A[1]~a[n-2] the same method of processing a round, and so on. A[1], a[2 ] 、...... a[n] are sorted in ascending order after the n-1 round is processed. two. Advantages and disadvantages Pros: stable;
Disadvantage: Slow, only two adjacent data can be moved at a time.

bubble 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.