The simple Exchange method of C language sorting algorithm, direct selection sort, bubble sort

Source: Internet
Author: User

C language sorting algorithm of the simple exchange of sorting, direct selection of sorting, bubble sort, the recent test to use, there are many examples on the Internet, I think it is to write their own to understand some.

  1. Simple Exchange Method sorting
    1 / * Simple Exchange MethodSort2 The position of the two records in the sequence based on the comparison of the two record key values in the sequence3 interchange sorting is characterized by moving a record with a large key value to the end of the sequence, and a record with a smaller key value moving toward the front of the sequence4 not stable5 */6#include <windows.h>7#include <stdio.h>8 voidMain () {9     inti,j,arr[Ten]={ -, $, -, at,Ten,9, -, About,3, the},t;Ten     intSize =sizeof(arr)/sizeof(int);//calculate the size of an array One      for(i =0;i< size-1; i++) {//N Number of n-1 wheel comparison A          for(j = i+1;j< size;j++)//when comparing each round, the following number is compared with the number i is the subscript -             if(Arr[j]<arr[i]) {//If I is a number larger than the number of subscripts, the two-digit exchange position -t=Arr[j]; thearr[j]=Arr[i]; -arr[i]=T; -             } -     } +      -      for(i =0;i< size;i++) {//Output +printf"%d", Arr[i]); A     }  at      -System"Pause"); -}

  2. Direct Select sort
    1 /*Select Sort2 each time a minimum (or maximum) element is selected from the data element to be sorted, it is stored at the beginning of the sequence until all the data elements to be sorted are exhausted. 3 Select Sort is an unstable sort method (for example, sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move to the second 5 back)4 */5#include <windows.h>6#include <stdio.h>7 voidMain () {8     intmin,i,j,arr[Ten]={ -, $, -, at,Ten,9, -, About,3, the},t;9     intSize =sizeof(arr)/sizeof(int);//calculate the size of an arrayTen      for(i =0; i< size-1; i++) {//N Number of n-1 wheel comparison Onemin = i;//Each round assumes I is the lowest value of the subscript and records the subscript A          for(j = i+1; J < size;j++)//when comparing each round, the following number is compared with the number of min as the subscript -             if(Arr[j]<arr[min]) min = j;//The following number is smaller than min for subscript, change min -         if(min! = i) {//if the value of min is changed, the number of the current subscript is not the minimum value, the interchange thet =Arr[i]; -arr[i]=Arr[min]; -Arr[min] =T; -         } +     } -      +      for(i =0;i< size;i++) {//Output Aprintf"%d", Arr[i]); at     }  -      -System"Pause"); -          -}

  3. Bubble sort
    /*Bubble sort repeatedly visited the sequence to sort, comparing two elements at a time, swapping them out if they were in the wrong order. The work of the sequence of visits is repeated until no more exchange is needed, i.e. the sequence is sorted*/#include<windows.h>#include<stdio.h>voidMain () {intmin,i,j,arr[Ten]={ -, $, -, at,Ten,9, -, About,3, the},t,noswap; intSize =sizeof(arr)/sizeof(int);//calculate the size of an array     for(i =0; i< size-1; i++) {//N Number of n-1 wheel comparisonNoswap =1;//flag whether an interchange occurs to avoid reordering ordered sequences         for(j =0; j<size-1-i;j++){             if(arr[j]>arr[j+1]){//If the previous number is larger than the latter, the Exchanget=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=T; Noswap=0;//there was an exchange.            }        }                 if(Noswap) Break;//No swap has occurred, instructions are ordered, no reordering, exit loops    }         for(i =0;i< size;i++) {//Outputprintf"%d", Arr[i]); } System ("Pause"); }

The simple Exchange method of C language sorting algorithm, direct selection sort, bubble sort

Related Article

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.