Improved insert sorting

Source: Internet
Author: User

Improved insert sorting

/* Note: The insert sorting method extracts a value from the front end of the half-end of the unordered sequence and inserts the appropriate position of the first half of the sorted sequence. The concept is simple but the speed is not high. One of the basic principles of sorting acceleration is to make the results of the previous sorting as much as possible to speed up the sorting, shell sorting method is based on this concept to improve the insert sorting method. Solution: slightly */# include <stdio. h> # include <stdlib. h> # include <time. h> # define MAX 10 # define SWAP (x, y) {int t; t = x; x = y; y = t;} void shellsort (int []); int main (void) {int number [MAX] = {0}; int I; srand (time (NULL); printf ("Before sorting: \ n "); for (I = 0; I <MAX; I ++) {number [I] = rand () % 100; printf ("% d", number [I]);} shellsort (number); // system ("pause"); return 0;} void shellsort (int number []) {int I, j, k, gap, t; gap = MAX/2; printf ("\ n after sorting: \ n"); while (gap> 0) {for (k = 0; k <gap; k ++) {for (I = k + gap; I <MAX; I + = gap) {for (j = I-gap; j> = k; j-= gap) {if (number [j]> number [j + gap]) {SWAP (number [j], number [j + gap]) ;}else {break ;}}}} printf ("\ n gap = % d:", gap); for (I = 0; I <MAX; I ++) {printf ("% d ", number [I]);} printf ("\ n"); gap/= 2 ;}}

 

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.