Linear time Sorting in linear time O (n)

Source: Internet
Author: User

Linear time Sorting in linear time O (n)

Sorting In Linear Time


I have tried many sort algorithms before, all of which are based on the comparison algorithm (base on comparing)

Collection of algorithm for sorting (part one)

Http://blog.csdn.net/cinmyheart/article/details/39268783

Collection of algorithm for sorting (part two)

Http://blog.csdn.net/cinmyheart/article/details/39396651

Collection of algorithm for sorting (part three)

Http://blog.csdn.net/cinmyheart/article/details/39413037


Certificate ------------------------------------------------------------------------------------------------------------------------------------

Counting Sort count sorting with a time complexity of O (n)


The implementation versions in two languages are provided here.


C language implementation:

/********************************************************* Code writer : EOF Code date   : 2014.01.11 Code file   : counting_sort.c e-mail      : jasonleaster@gmail.com Code description:   Here is a implementation for counting sort.   If you find something wrong with my code, please touchme by e-mail.*********************************************************/#include 
 
  #include 
  
   int counting_sort(int *num,int size){    if(!num)    {        return -1;    }    int *p_buffer = (int*)malloc(sizeof(int)*size);    //initialization    int i = 0;    for(i = 0; i < size; i++)    {        p_buffer[i] = 0;    }    //counting    for(i = 0; i < size; i++)    {        p_buffer[num[i]] += 1;    }    //output from bigger element to smaller element    int index = 0;    for(index = i = size -1;i >= 0; i--)    {        while(p_buffer[i]-- > 0)        {            num[(size -1) - index] = i;            index--;        }    }    free(p_buffer);    return 0;}int main(){    int array[] = {1,3,4,3,2,7,4,0};    int size_array = sizeof(array)/sizeof(array[0]);    int i = 0;    printf("Before sorting:array[] = \n");    for(i = 0; i < size_array; i++)    {        printf("\t%d",array[i]);    }    printf("\n");    counting_sort(array,size_array);    printf("After sorting:array[] = \n");    for(i = 0; i < size_array; i++)    {        printf("\t%d",array[i]);    }    printf("\n");    return 0;}
  
 




Python implementation: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> "" Code writer: EOF Code date: 4.04.01.11 Code file: counting_sort.py e-mail: jasonleaster@gmail.com Code description: here is a implementation for counting sort. if you find something wrong with my code, please touchme by e-mail. "def counting_sort (array): size = len (array) buf = [0] * size for I in range (0, size ): buf [array [I] + = 1 for index in range (size-1, 0,-1): I = index while buf [I]> 0: array [(size-1)-index] = I index-= 1 buf [I]-= 1 return arrayarray =, 4, 0] print "Befor sorting", arraysorted_ary = counting_sort (array) print "After sorting", sorted_ary




Radix sorting wait for updating...




Not to the east, nor to the west, but to the heart


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.