Sequencing of C programming

Source: Internet
Author: User

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 typedef struct node{5 int  NUM[10];  6 int length;  7 Char s[10];  8}pnode; 9//exchange one void swap (Pnode *l,int m,int n) {int temp=0; Temp=l->num[m] ; l->num[m]=l->num[n]; l->num[n]=temp;            +//bubble Sort bubblesort (Pnode *l) {+ int i=1; int j=1; 23                   For (j=1;j<=l->length;j++) (i=l->length-1;i>=j;i--) 26                   {if (l->num[i]>l->num[i+1]) (l,i,i+1); 29 }//simple Selection Sort//straight Insertion Sort 37 38 39) Heap sort heapsort (Pnode *l) (I=1,//for  )This is OK!!!! (i=l->length;i>0;i--) Heap_adjust (l,i,l->length);                    i=l->length;i>1;i--/For (i=l->length/2;i>0;i--) 48 {49 Swap (l,1,i); Heap_adjust (l,1,i-1); //heap adjust heap_adjust void (Pnode *l,int s,int m) (mm) NT Temp=0; j=0 int; temp=l->num[s]; J=2*s;j<=m;j=j*2 (j<m&& (l->num[j]<l-&) GT;NUM[J+1]) ++j; if (Temp>l->num[j]); l->num[s]=l->num[j]; S=j; l->num[s]=temp;} The//merging sort mergesort (Pnode *l) (Msort),L->num,1,l->length); Msort (int num1[],int num2[],int m,int N), +--INT--num3[ 11];      if (m==n) Bayi {num2[m]=num1[m]; This was the recursion back condition, the lag= (m+n)/2; Msort (Num1,num3,m,flag); Msort (Num1,num3,flag+1,n); The Num3,num2,m,flag,n Merge (); (int num1[],int num2[],int m,int flag,int N), [94 i] NT J,k,l; j=flag+1,k=m;m<=flag&&j<=n;k++ (NUM1[M]&LT)                    ; Num1[j]) 98 {num2[k]=num1[m++];100}101    else102 {103 num2[k]=num1[j++];104                     }105}106 if (m<=flag) 107 {108 for (                 l=0;l<=flag-m;l++) 109 num2[k+l]=num1[m+l];110}111 if (j<=n) 112                 {113 for (l=0;l<=n-j;l++) num2[k+l]=num1[j+l];115             }116}117//quick sort118 void Qsort (Pnode *l,int low,int High) 119 {int flag;121 if (Low

The main realization is bubble sort, heap sort, merge sort, quick sort. The recursive algorithm is applied to merge sort and quick sort.

Bubble sort: The main is to use a double loop, the first layer of the loop to control the overall order of the step, the first layer of the loop on the basis of a larger cycle than the beginning of the order table from the end of the comparison, the smallest or largest number of rows to the start of the current layer of the loop.

Heap Sort

A heap is a complete binary tree with the following properties: Each node's value is greater than or equal to the value of its left and right child nodes, called the Big Top heap, or the value of each node is less than or equal to the value of its left and right children, called the small top heap.

Step 1, the data is built into a large top heap, with the use data (i) >=data (2i), data (i) >=data (2i+1) is to ensure that each node is greater than its children

Step 2, according to step 1 set up the large top heap, we can know that the full binary tree top heap is the maximum value, then we put the maximum value to the bottom of the tree, so that the largest number on the last side of the exchange, We have to change the bottom of the number to the top of the stack will be inconsistent with the big top heap data structure so we need to adjust. such as code:

//heap adjust si void Heap_adjust (pnode *l,int s,int m) (temp=0);             Um[s]; 62             (j=2*s;j<=m;j=j*2) (                    j<m&& (l->num[j]<l->num[j+1))                         ++j                    if (Temp>l->num[j]) a break                         ;                    l->num[s]=l->num[j];                         s=j;                    l->num[s]=temp;         

Our arguments for the Heap_adjust (Pnode *l,int s,int m) function are Heap_adjust (L,1,I-1), which is the equivalent of s=1,m=i-1, and we adjust the range from the top to the bottom of the data we just exchanged, minus 1, or 1. <=x<i-1;

The purpose of the adjustment is to ensure that the current complete binary tree is a large top heap, the top number must be the largest, only in this way we can continue to the top of the data exchange to the end. The method of adjustment is to find the node and then find the child in this node, so the change is in multiples of 2, only in this way to meet the data (i) >=data (2i), data (i) >=data (2i+1) this formula.

Merge sort

Merge sort: Assuming that the initial sequence contains n records, it can be regarded as n ordered sequence, each subsequence has a length of 1, and then merges to get N/2 with a length of 2 or 1 ordered subsequence; Until a sequential sequence of length n is obtained, this sort method is called a 2-way merge sort.

Step 1, the data provided is divided into two paragraphs, if we provide data is 50, 10, 90, 30, 70, 40, 80, 60, 201 a total of 9 numbers

The first time you call the Msort (num1,num3,m,flag) function, m=1,flag=9 divides 1-9 into 1-5, 6-9, and 1-5 to continue iterating over m=1,flag=5, dividing 1-5 into 1-3, 4-5, and then 1-3 to continue iterating m= 1,flag=3, divides 1-3 into 1-2, 3-3, and then 1-2 continues to iterate m=1,flag=2, divides 1-2 into 1-1, 2-2 then we have a recursive return judgment Num2[m]=num1[m], at which time m=1,n=1 recursively returns the first time, Program execution 1-2 within the range of 1-1 of the other half of the 2-2,m=n recursively return the second, and then in the two number call merge () function to do a merge, the first merge, so that 1-2 divided into two segments to complete the merge.

Then the program will continue to execute is the function from the 1-3 range of the other half 3-3, because M=n so recursive return, directly perform 1-2,3-3 merge, so that 1-3 completed the merge.

Recursive continuation return should be 1-5 in the range of the other half 4-5 to make a recursive call, because 1-3 we have completed the merge, m=4,n=5 4-5 into 4-4, 5-5, that is, the data segment of the two segment are returned directly recursively, and then the s=4,s=5 into the num2 array: num2[m]= NUM1[M]; This was the recursion back condition, so num2[4], num2[5] have stored the value, then the program has executed 87 lines, 88 lines of code, so the program continues to execute 89 lines of code, that is, num2[4], num2[5] To do a merge, and then recursively return the 1-3,4-5 two data calls 89 lines of code to do a merge, so that 1-9 divided into two segments 1-5, 6-9 the first half of the sequence has been completed, the program has been recursively returned to the first execution of the Msort function, the state, That is, the first call to the Msort function is divided into 1-5, 6-9 segments here, equivalent to all previous steps in the first entry into the Msort function after the 1-9 into the 1-5,6-9 two segment, and then m=1,flag=5 call 87 lines of code, the recursive call returned after the execution of 88 lines of code.

After the program continues to execute 88 lines of code and then repeat data segment 1-5 to complete 6-9 of the data sort, until 88 lines of code recursively return the last only 1-5, 6-9 segments of the data merge then the execution of 89 lines of code to complete the final data merge completed the entire merge sort. It can be seen that the whole process is constantly in the segmentation, merging, and finally the program recursively return to the original segment and then merge two long data segments to complete the merge sort. So when we write the program when the merge function we can imagine that this is already two good big data segment, we need to merge the two data segments, so that the variable range of variables inside is very good judgment, and the idea immediately clear a lot, if the bypass recursion inside will be very complex, Also note that this msor function has two recursive calls, the order of execution is a call msort function, will contain all the contents of this function, that is, 87 rows of the function of each recursive return will call once 88, 89 lines of code and then complete a merge, Because a function call in the program will form a stack frame, this stack frame contains all the contents of the function including variables, function return address and so on, so the Msort function returns once per recursive, and then executes the subsequent code, that is, another range of data segments, Then call 89 lines of code to complete merging the two data segments.

                   Msort (Num1,num3,m,flag);                   Msort (num1,num3,flag+1,n);                   Merge (Num3,num2,m,flag,n);

Quick Sort

Quick sort: By dividing the pending records into two separate parts by a single pass, in which some of the recorded keywords are smaller than the keywords in the other part of the record, the two parts of the records can be sorted separately to achieve the order of the whole sequence.

The key point of the fast line is constantly changing the position of the keyword, that is, the number of the left side of the keyword is less than the keyword, the number on the right is greater than the keyword, changing the position of the keyword is finally finished sorting.

    

Sequencing of C programming

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.