C language-Hao bin notes-006 Sorting and finding

Source: Internet
Author: User

1.

1 intPartion (int*a,intLowintHigh )2 {3    intValue =A[low];4    intT;5 6     while(Low <High )7    {8        while(High > Low && A[high] >=value)9high--;//jumps to the 8-row while to execute, instead of going to line 10 to execute the IF statementTen       if(High! =Low ) One       { At =A[low]; -A[low] =A[high]; -A[high] =T; the       } -  -        while(Low < High && A[low] <=value) -low++; +        -       if(Low! =High ) +       { At =A[low]; atA[low] =A[high]; -A[high] =T; -       } -    } -    returnLow ; -}

2.

1 /*2 IBM < Data structure > Describes a faster bubble sorting method than the normal bubble sort method3 */4 5# include <c:\turboc2\Ten-2\ziliao\sort\hh.c>6 7 /*The function is still the bubble sort, the only difference is that we add a flag flags, once the array is found8 elements are not exchanged, we can roll out the cycle in advance, saving time!9 Ten */ One voidSortint*a,intN/*Bubble Ascending Sort*/ A { -     intI, flag =1, J; -     inttemp; the      -I=1; -      while(flag) -     { +Flag =0; -          for(j=0; j<n-i; ++j) +         { A             if(A[j] > a[j+1]) at             { -temp =A[j]; -A[J] = a[j+1]; -                 Continue;//will jump to execute ++j -a[j+1] =temp; -Flag =1; in                  Break;//will jump out of the for loop and go to the 32-line break statement -             }  to         } +          Break;//2 jumps out while loop -++i; the     } * } $ Panax Notoginseng  - Main () the { + clrscr (); A      theprintf"The array is:\n"); +Prin (A,Ten); -Sort (A,Ten); $printf"The sorted array is:\n"); $Prin (A,Ten); -      - getch (); the     return 0; - }Wuyi  the /* - last modified in 07 lunar four night 21:15 remote Internet cafes! Wu  - */

3.

1 /*2 2007-5-213 binary Lookup Algorithm "recursive method to achieve"4 */5 6# include <stdio.h>7 8 /* 9 p points to the first element of the array, n indicates a number of lengths, Val is the element to be searched, returns the subscript of the element if found, otherwise returns-1 tableTen Show did not find hehe! One */ A intFind (int* p,intLowintHighintval) - { -     intMid = (low + high)/2; the      -     if(Low = = high)/*there is no low > high condition!*/ -     { -         if(P[mid] = =val) +         { -             returnmid; +         } A         Else at         { -             return-1;  -         } -     } -     Else -     {     in         if(P[mid] <val) -         { toFind (p,mid+1, high,val); +         } -         Else if(P[mid] >val) the         { *Find (p,low,mid-1, Val); $         }Panax Notoginseng         Else if(P[mid] = = val)/*finally this else. If do not miss out, of course if (p[mid] = = val) also can not write*/ -         { the             returnmid;  +         } A     } the      +printf"John Doe!\n"); - } $  $  -  - voidTraverse (int* p,intN) the { -      for(intI=0; i<n; ++i)Wuyiprintf"%-5d", P[i]); the      -printf"\ n"); Wu } -  About  $ intMainvoid) - { -     inta[Ten] = {-Ten,1, at, Wu, -, the, the, -, $,9897}; -      APuts"the contents of the original array are:"); +Traverse (A,Ten); the      -     intK = Find (A,0,9,-Ten); $     if(-1==k) theprintf"the element was not found!!! \ n"); the     Else theprintf"the exact location of the element is%d \ n", k); the      -     return 0; in } the  the /* About the first thing to understand, the The binary lookup is based on the assumption that the elements in the array are already sorted (regardless of ascending or descending order) . the      the */

C language-Hao bin notes-006 Sorting and finding

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.