Common sorting algorithm (C language) of a

2010-12-01  来源:本站原创  分类:Tech  人气:103 

Data structure has been reviewed recently and felt that even the scientific point of C to be lost, spent a little time to achieve some sort count a bit, talk to encourage each other.

Direct Insertion Sort:

void insertsort(int *q)/* Insertion sort  */
{
        int i,j,k;
        for(i=2;i<LENGTH;i++)
        {
                q[0]=q[i];/* The first unit acts as a sort of Sentinel, does not participate in  */
                j=i-1;
                while(q[0]<q[j])/* Find the right insert location  */
                {
                        q[j+1]=q[j];
                        j--;
                }
                q[j+1]=q[0];
                printf("\n--------------------------------------\n");/* Print each trip for sorting results  */
                for(k=1;k<LENGTH;k++)
                {
                        printf("%d ",q[k]);
                }
        }
}

Binary insertion sort:

void binaryInsertSort(int *q)/* Binary insertion sort  */
{
        int mid,low,high;
        int i,j;
        for(i=2;i<LENGTH;i++)
        {
                q[0]=q[i];
                low=1;
                high=i-1;
                while(low<=high)/* Find to be inserted  */
                {
                        mid=(low+high)/2;
                        if(q[0]<=q[mid])
                                high=mid-1;
                        else
                                low=mid+1;
                }
                for(j=i-1;j>=high+1;j--)
                        q[j+1]=q[j];
                q[high+1]=q[0];
                printf("\n--------------------------------------\n");/* Print each trip for sorting results  */
                for(k=1;k<LENGTH;k++)
                {
                        printf("%d ",q[k]);
                }
        }
        }
}

Bubble Sort:

void bubbleSort(int*q)/* Simple bubble sort  */
{
        int i,j;
        int temp;/* Exchange mediator  */
        for(i=1;i<LENGTH;i++)
        {
                for(j=LENGTH-i;j>=i;j--)
                {
                        if(q[j]<q[j-1])
                        {
                                temp=q[j];
                                q[j]=q[j-1];
                                q[j-1]=temp;
                        }
                }
        }
}

Simple Selection Sort:

void selectSort(int*q)/* Select sort  */
{
        int i,j;
        int k;// Indicates that each time you find the minimum value of the location
        for(i=1;i<LENGTH;i++)
        {
                k=i;
                for(j=i;j<LENGTH;j++)/* Find current interval the minimum '  */
                {
                        if(q[j]<q[k])
                                k=j;
                }
                q[0]=q[k];
                q[k]=q[i];
                q[i]=q[0];
        }
}

Test the main function:

int main()
{
        int a[LENGTH];
        int i,j;
        int *p;
        printf("please input 10 number:\n");
        for(i=1;i<LENGTH;i++)/* Enter 10 digit  */
        {
                scanf("%d",&a[i]);
        }
        printf("the number you have input is:--------------------------\n");
        for(j=1;j<LENGTH;j++)
        {
                printf("%d",a[j]);
                printf(" ");
        }
        p=a;
        insertSort(p);/* According to the need to call you want to test sorting function  */
        printf("\n----------------------insertsort result------------------\n");
        for(j=1;j<LENGTH;j++)
        {
                printf("%d",a[j]);
                printf(" ");
        }

        /*binaryInsertSort(p);
        printf("\n----------------------binaryinsertsort result------------------\n");
        for(j=1;j<LENGTH;j++)
        {
                printf("%d",a[j]);
                printf(" ");
        }*/

        /*bubbleSort(p);
        printf("\n----------------------bubblesort result------------------------\n");
        for(j=1;j<LENGTH;j++)
        {
                printf("%d",a[j]);
                printf(" ");
        }*/

        /*selectSort(p);
        printf("\n----------------------selectsort result------------------------\n");
        for(j=1;j<LENGTH;j++)
        {
                printf("%d",a[j]);
                printf(" ");
        }*/
        return 0;
}

The first part to be written here, the second part includes Hill sort (narrow increments), quick sort, merge sort, heap sort, radix sort, paste a few days out.

相关文章
  • Common sorting algorithm (C language) of a 2010-12-01

    Data structure has been reviewed recently and felt that even the scientific point of C to be lost, spent a little time to achieve some sort count a bit, talk to encourage each other. Direct Insertion Sort: void insertsort(int *q)/* Insertion sort */

  • Common sorting algorithm stability analysis algorithm stability 2010-04-05

    First, the sorting algorithm stability we should all know, is the guarantees More simply order the number of former two equal before and after its position in the sequence order and sorted them both before and after the location of the same order. In

  • Common sorting algorithm summary 2010-08-25

    Some time ago was busy looking for internships, interviewing many companies, both large and small companies are paying more attention to the basics of the study. Now pumping point blank to sum up the interview and little, some things can be found eve

  • Common sorting algorithm on the stability analysis and conclusions 2011-01-14

    First, the sorting algorithm stability we should all know, is that we can ensure that More simply sort the numbers before the two equal before and after its position in the sequence order and sorted them both before and after the position of the same

  • JavaScript version of the common sorting algorithm 2009-06-07

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312"> <head> <title&

  • Common sorting algorithm 2010-06-07

    A simple sorting algorithm 1. Bubble method: This is the most original, and also well-known algorithm was the slowest. The origin of his name because it seems like the work of the bubble: In a group of numbers to be sorted in order of current has not

  • Several common sorting algorithm 2011-05-27

    Java language with a variety of sorting, including insertion sort, bubble sort, selection sort, Shell sort, quick sort, merge sort, heap sort, SortUtil and so on. Insertion Sort: package org.rut.util.algorithm.support; import org.rut.util.algorithm.S

  • Common sorting algorithm java implementation 2010-09-04

    1, bubble sort Java code package sort.bubble; import java.util.Random; / ** * Comparison between two consecutive numbers in turn, will put decimal in front of large numbers on the back * Bubble sort, with stability * Time complexity is O (n ^ 2) * Le

  • Flex common sorting algorithm summary 2010-09-05

    There are many sorting algorithms, which in the Flex Array class has sort method, mx.collection package which also Sort category, check under the specific use of API's enough. Original aim, on the basis of summing up the relative ranking of 5 algorit

  • Java three common sorting algorithm: bubble, choose, insert 2010-08-06

    /** * * @author Administrator: AFO ([email protected]) * */ public class BubbleSorting { public static void main(String args[]) { //------------------------- Bubble sort // Initial array . int[] list = { 1, 5, 4, 2, 3, 8, 9, 7, 6, 12 }; // Displays th

  • JS common sorting algorithm 2010-08-31

    http://bokee.shinylife.net/blog/article.asp?id=370 <html> <head> <script> Array.prototype.swap = function (i, j) ( var temp = this [i]; this [i] = this [j]; this [j] = temp; ) Array.prototype.bubbleSort = function () ( for (var i = this.

  • 11 kinds of commonly used sorting algorithm Glance 2011-05-06

    Under the sorting algorithm recently learned, but also the realization of a mess a little, here will I have to master the 11 kinds of common sorting algorithms are summarized as follows (article I of the order of unity described by small to large) Th

  • Interview in sorting algorithm 2010-12-01

    Order to make a common sorting algorithm: Recommend the use of the written selection sort, quick sort interview recommended Stability: towards collating sequence, the value equal to the record, before and after the relative order of the same sort, ca

  • Related sorting algorithm 2010-12-01

    Order to make a common sorting algorithm: Recommend the use of the written selection sort, quick sort interview recommended Stability: towards collating sequence, the value equal to the record, before and after the relative order of the same sort, ca

  • Summary of the classical sorting algorithm 2010-12-10

    Busy for some time before the written examination of each company, almost all IT companies will be involved in his written introduction to data structures and algorithms such stuff, Sohu's pen questions also appear in a combination of mathematics con

  • [Reserved] ---- distribution sorting algorithm sort (me sort, radix sort, comparison) 2010-10-05

    The basic idea of distribution sorting: sorting process without comparing keywords, but by "distribution" and "gathering" process to achieve the sort. They can achieve linear time complexity order: O (n). Box Sort (Bin Sort) 1, the bas

  • Simple summary-simple sorting sorting algorithm 2010-04-25

    Web development since the beginning has had much contact algorithm, and taking advantage of the time available, will be studied before the algorithm re-sort again, from the simplest sorting algorithm start, bubble sort, selection sort, insertion sort

  • 100 million floating point sorting algorithm 2010-05-04

    There are 100 million floating point, please find out the minimum 10,000. Hint: Assume that each float, 4 bytes, 100 million floating-point numbers will have to stand to the considerable space and can not be read into memory all at once to sort. Anal

  • sorting algorithm java version of their summary 2010-05-26

    Almost started looking for work, so today, on the sorting algorithm specifically review, record some ideas. First presented the theory and implementation of various algorithms, and finally concluded to do it. 1. Bubble sort, this should be familiar t

  • Java sorting algorithm 2010-06-14

    package com.wepull.jbs.lesson4; import java.util.Random; / ** * * Sort Test class * * * * Sorting algorithm is as follows: * * 1. Insertion Sort (direct insertion sort, binary insertion sort, Shell sort); * * 2. Exchange sort (take bubble sort, quick