Sorting and searching of data structures

Source: Internet
Author: User

 

Bubble SortingIt is to call a small element forward or a large element backward. The comparison is an adjacent comparison between two elements, and the Exchange also occurs between these two elements. Therefore, if the two elements are equal, I think you will not be bored to exchange them. If the two equal elements are not adjacent, even if the two are adjacent through the previous two exchanges, at this time, the sequence of the same elements is not changed, so the Bubble Sorting is a stable sorting algorithm.

The Bubble Sorting Algorithm operates as follows:
Compares adjacent elements. If the first is bigger than the second, exchange the two of them.
Perform the same operation on each adjacent element, from the first to the last. At this point, the final element should be the largest number.
Repeat the preceding steps for all elements except the last one.
Continue to repeat the above steps for fewer and fewer elements until no one pair of numbers needs to be compared.

Void maopao (int * arr, int n) {int j, I; int temp; for (j = n-1; j> 0; j --) {for (I = 0; I <= j; I ++) {if (arr [I]> arr [I + 1]) {temp = arr [I]; arr [I] = arr [I + 1]; arr [I + 1] = temp ;}} printf (Bubble Sorting :); for (int k = 0; k
 
  
0; j --) {for (I = 0; I
  
   
Puke [I + 1]) // 0----3 1----4 {temp = puke [I + 1]; // 1 ---- 4 puke [I + 1] = puke [I]; // 1----40 ---- 3 puke [I] = temp; // 0---3 }}printf (bubble sort 2 :); for (I = 0; I
   
    

    Select sort:
    
The Direct selection and sorting of files with n records can be directly selected and sorted through n-1 rows to get the ordered results:
① Initial status: the disordered area is R [1. n], and the ordered area is empty.
② Sorting by 1st bits
In the unordered zone R [1 .. n] selects the record R [k] with the minimum keyword, swaps it with the 1st records R [1] In the unordered area, so that R [1 .. 1] and R [2 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count.
......
③ Sort by I
At the beginning of the I-th sorting, the current ordered and disordered areas are R [1 .. I-1] and R (I.. n) respectively ). This sort field selects the record R [k] with the smallest keyword from the current unordered area and exchanges it with the R of the 1st records in the unordered area so that R [1 .. i] and R change to a new ordered area with one more record count and a new unordered area with one fewer record count.
Common sorting methods include simple sorting, tree sorting (tournament sorting), and heap sorting. The preceding algorithm is only a simple step for sorting.
Void selete (int * arr, int n) {int s; int temp; int I, j; for (j = 0; j
     
      
Arr [I]) {s = I;} temp = arr [s]; arr [s] = arr [I]; arr [I] = temp ;}} printf (select sort :); for (int I = 0; I
      
       

       Insert sorting:
       
Divides the series of n elements into two parts: ordered and unordered, for example
Insert sorting process example
Insert sorting process example
As shown below:
{A1}, {a2, a3, a4 ,..., An }}
{A1 (1), a2 (1)}, {a3 (1), a4 (1 ..., An (1 }}
...
{A1 (n-1), a2 (n-1 ),...}, {An (n-1 )}}
Each processing is to compare the first element of the unordered series with the elements of the ordered series one by one from the back to the front to locate the insertion position and insert the element into the appropriate position of the ordered series.
Void insert_sort (int * arr, int n) {int m = sizeof (n)/sizeof (int); int I, j; int temp; int min; for (I = 2; I
        
         
= 0; j --) {if (arr [j]> arr [j + 1]) {temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp ;}} printf (insert sort 2 :); for (int k = 0; k
         
          Binary Search:The half-lookup method is also called the binary lookup method. It fully utilizes the order relationship between elements and adopts the grouping policy. In the worst case, it can use O (log n) to complete the search task. The basic idea is to divide n elements into two halves with roughly the same number. Take a [n/2] for comparison with x to be searched, if x = a [n/2], locate x and terminate the algorithm. If xa [n/2] is returned, we only need to search for x in the right half of array. The binary search method generally has a critical BUG, that is, the last or first value cannot be found. When we compare the last two values, we can judge again which value is equal to the searched value.
          

 

 

int  find_binary(int *arr,int n,int num){    int mid,low,high;low=0;high=n-1;int position=0;if(n<1){return -1;}mid=(low+high)/2;if(num==arr[mid]){position=mid;}else if(numarr[mid]) {    int temp=find_binary(arr+mid+low,n-mid-1,num);        if(temp!=-1)position=temp+mid+1;}   return position;}

Source code of all programs:

 

 

# Include
           
            
# Include
            
             
# Include
             
              
Void maopao (int * arr, int n) {int j, I; int temp; for (j = n-1; j> 0; j --) {for (I = 0; I <= j; I ++) {if (arr [I]> arr [I + 1]) {temp = arr [I]; arr [I] = arr [I + 1]; arr [I + 1] = temp ;}} printf (Bubble Sorting :); for (int k = 0; k
              
               
0; j --) {for (I = 0; I
               
                
Puke [I + 1]) // 0----3 1----4 {temp = puke [I + 1]; // 1 ---- 4 puke [I + 1] = puke [I]; // 1----40 ---- 3 puke [I] = temp; // 0---3 }}printf (bubble sort 2 :); for (I = 0; I
                
                  Arr [I]) {s = I;} temp = arr [s]; arr [s] = arr [I]; arr [I] = temp ;}} printf (select sort :); for (int I = 0; I
                 
                   = 0; j --) {if (arr [j]> arr [j + 1]) {temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp ;}} printf (insert sort 2 :); for (int k = 0; k
                  
                    Arr [mid]) {int temp = find_binary (arr + mid + low, n-mid-1, num); if (temp! =-1) position = temp + mid + 1;} return position;} int main () {int arr [] = }; int arr2 [] = {5, 7, 2, 3, 1}; // int n = 6; // (malloc (strlen (arr) + 1)/sizeof (int )); int m = sizeof (arr)/sizeof (int); int n = sizeof (arr2)/sizeof (int); printf (number of sorted digits: % d, n ); printf (number of search numbers: % d, m); maopao (arr, n); // Bubble Sorting printf (); maopao2 (arr, n ); // Bubble Sorting 2 printf (); selete (arr, n); // select the sorting printf (); insert_sort (arr, n); // Insert the sorting printf (); insertSort2 (arr, n); // Insert the second method of sorting printf (); int position = find_binary (arr, m, 4); // query printf in two copies (location: % d, position); printf (); return 0 ;}
                  
                 
                
               
              
             
            
           

Running result:

 

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.