Common sorting Algorithms (v) Cardinal sort, bucket sort, and count sort

Source: Internet
Author: User

This is a sorting algorithm of three linear time complexity, which is the first and the cardinal sort of the sort order using an operation rather than a comparison 1. Introduction

It is an entirely different sort method from other sorting algorithms, and the other sorting algorithms are done by comparing and moving between keywords, and it is the idea of using a multi-key word.

The idea of multiple keywords: given a set of data, I can sort all the numbers by the size of a single digit, and then sort by 10 bits, all the way up to the highest level, so that the whole set of data becomes valid, so that the method starting from the lowest bit is called the lowest bit first .

2. Schematic process

After a single placement and recycling, the resulting sequence has been ordered in bits, followed by the next low-level placement and recycling.

As you can see, if the largest number in a set of sequences is two digits, then two allocations and collections are required, and the total number of allocations collected is related to the number of digits of the maximum number

The base sort requires two auxiliary spaces, one is the 0~9 bucket, the other is an array of computed positioning, and what is the positioning array? is to record where the data in each bucket will be put back in the original array.

def radixsort (Number:array[int], d:int): Unit = {//D Indicates the maximum number of bits    var k = 0    var n, m = 1    //Control key value sorted by which    VA L TEMP = Array.ofdim[int] (ten, number.length)    //The first dimension of the array represents the possible remainder 0-9    val order = new Array[int] (10)//array orderp[i] The number used to indicate that the bit is I (    m <= d) {for      (i <-number.indices) {        val LSD = (number (i)/n)%        temp (LSD) ( Order (LSD)) = number (i)        order (LSD) + = 1      }      var i = 0 while      (i <) {        if (order (i)! = 0) {
   
    var j = 0 While          (J < Order (i)) {number            (k) = temp (i) (j)            k + = 1            J + = 1          }        }        order (i) = 0        i + = 1      }      n *=      k = 0      m + = 1    }  }
   

The time complexity of radix sorting can be understood as O (d*n), D is the largest number of digits in the sequence, and is suitable for sequences with a large n value but smaller keywords.

Ii. sequencing of barrels 1. Concept

The bucket sort divides the [0,1] interval into a sub-interval of n equal size, which is called a bucket. The n input elements are then placed in their respective buckets respectively. Because the input is uniform and independent, so there will not be many numbers at the same time falling in a bucket of the case. In this way, we want to sort the data in each bucket, and then walk through each bucket, listing the elements in each bucket in order.

2. Feature description

1 ". Bucket sequencing time complexity is usually O (N+N*LOGM), where N is the number of buckets, m represents the number of elements in the bucket (here, M is a approximate average, this also shows why the elements in the bucket as far as possible do not appear a lot, and some of the uneven distribution of things, the distribution of uneven words, The performance benefits of the algorithm cannot be maximized).

2 ". Bucket sequencing is stable (it can be done in a balanced sort).

3 ". Bucket sequencing, in memory consumption is relatively large, it can be said that its time performance advantage is from the sacrifice of space exchange.

Bucket sequencing, in the case of big data volumes , is faster than fast sorting. If the number of data elements to be sorted is relatively small, the advantage of bucket ordering is not so obvious, because the bucket sort is based on divide-and-conquer strategy, the data can be distributed sorting, give full play to the advantages of parallel computing.

3. Steps

1. Find the maximum value in the array you want to sort max, Min min

2. We use dynamic array ArrayList as buckets, and the elements in the buckets are stored with ArrayList. The number of barrels is (max-min)/arr.length+1

3. Iterate through the array arr, calculate each element arr[i] put the bucket

4. Each bucket is sorted by its own

5. Iterate through the bucket array and put the sorted elements into the output array

4. Implement
def bucketsort (Inputdata:arraybuffer[int], max:int): arraybuffer[int] = {    var buckets = new Array[int] (max)    for (I <-inputdata.indices)//Count      buckets (Inputdata (i)) = Buckets (Inputdata (i)) + 1    var j = 0 for    (i <-0 u Ntil max) while      (buckets (i) > 0) {        inputdata (j) = i        j = j + 1        buckets (i) = buckets (i)-1      }    bu Ckets = null    inputdata  }
Third, counting sort 1. Advantages

Counting sorting is a special case of bucket ordering, which can be counted as a case of only one element in each bucket, it is a non-comparison based sorting algorithm, its advantage lies in the range of integers sorted, its complexity is 0 (n+k) (where K is the range of integers), faster than any comparison sorting algorithm.

2. Thought

For an element x in an input array, if there are n elements in the array that are smaller than x, then we can place the x directly in the (n+1) position. This is the basic idea of counting sorting, similar to the direct addressing method in a hash table, in a given set of sequences, we first find out the maximum and minimum values in the sequence so as to determine how much auxiliary space to open up, and each number has a unique subscript in the corresponding auxiliary space.
Based on this idea, one of the main problems with counting sorting is how to count the number of elements in an array . Plus the element in the input array is an integer of the 0-k interval, the value of the input element can be represented by the address of another array, and the value of the array represents the number of elements in the method to be counted.

The following is a method for counting the number of elements in an array where the array elements are all integers of the 0-k interval.

    1. Find the maximum and minimum values in the sequence, and open up the auxiliary space of max-min+1
    2. The minimum number corresponds to the position of the subscript 0, and a number is given to the corresponding subscript at the value of +1.
    3. Iterate through the secondary space to get an ordered set of sequences

3. Algorithm Analysis:

Counting sorting is a sort of space-time-based algorithm, and only applies when all the numbers in the sequence are concentrated, such as 0 1 2 3 4 999 in a set of sequences, and 1000 auxiliary spaces.
Complexity of Time
The time-of-order theory for the counting sort is O (n+k), where K is the range of the numbers in the sequence.
However, when O (k) >o (N*log (n)), it is less efficient than the comparison-based sorting (the time complexity of the comparison-based sorting in the theoretical lower bound is O (N*log (n)), such as merge sort, heap sort)

4. Code implementation
def countingsort (Inputdata:arraybuffer[int], k:int): array[int] = {    //k indicates that the input number is between 0 and K    val temp = new Array[in T] (k)    //temporary storage    val outdata = new Array[int] (inputdata.length)    val len = temp.length for    (i <-0 until L EN) {      //Initialize      temp (i) = 0    } for    (i <-inputdata.indices) {      temp (inputdata (i)) = Temp (Inputdata (i) ) + 1    }    for (i <-1 until len) {      temp (i) = temp (i) + temp (i-1)    }    //Put the elements in the input array in the corresponding position in the output array 
   var n = inputdata.length-1 while    (n >= 0) {      //from Backward through      outdata (temp (Inputdata (n))-1) = Inputdata ( N)      Temp (Inputdata (n)) = temp (Inputdata (n))-1      n = n-1    }    outdata  }

Common sorting Algorithms (v) Cardinal sort, bucket sort, and count sort

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.