"Aha algorithm"--sort

Source: Internet
Author: User

Today began the study of the "AHA algorithm" book. Generally speaking, this book is the algorithm of the small petition, the language is very popular, the introduction of the algorithm is relatively simple, now back to see the simple things will seem to waste time? Otherwise, I recently feel that the competition is not the ultimate way, the direction of learning is also changed to book as the object ("Specific mathematics" a column of the establishment can be seen), began to gradually improve the system of professional knowledge, and slowly do some practical things.

For the sorting problem, is the basic problem that the algorithm needs to solve, but the order also has many kinds, its different method also decided its different efficiency, below we look at several common sorts method.

Bucket sort:

Give a set of examples: Sort 5, 8, 3, 1, 7 from small to large.

The bucket sort is based on a one-dimensional array, given this sort method: Initialize array a[10] = 0, then a[5]++, a[8]++, a[3]++, a[1]++, a[7]++. Then we control the Loop statement, Access A[i], and I output A[I] times, and eventually we get a small to large arrangement. And this process, as its name describes, throws the data I into an array element labeled I a[i] in this bucket.

The order of the barrels skillfully the number I as the subscript of a one-dimensional array, and with a[i] record I occurrences of the number of times, using subscript from the first to the largest arrangement of the data to complete the arrangement. Its advantages are simple and clear, but the shortcomings are also obvious, we open the array length is limited by the memory space, so with the expansion of data, the sorting will be stretched.

Let's simply implement this sort of bucket.

#include <cstdio>intMain () {inta[ One], I, J, T;  for(i =0; I <=Ten; i++) A[i]=0;  for(i =1; I <=5; i++) {scanf ("%d",&t); A[t]++; }           for(i =0; I <=Ten; i++)               for(j =1; J <= a[i];j++) printf ("%d", i); return 0;}

"Aha algorithm"--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.