9.3.3 Hill sorting

Source: Internet
Author: User

Hill sorting is the optimization of direct insertion sorting. Both the number of comparisons and the number of inserts should decrease.

# Include <stdio. h> # include <stdlib. h> void swap (int arr [], int I, int j) {int temp; temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} void ShellSort (int arr [], int n) {int I; int increment = n/2; while (increment! = 0) {for (I = increment; I <n; I ++) // The loop starts from the first 2nd array elements, shift arr [0] as the first sorted part {int j; int temp = arr [I]; for (j = I-increment; j> = 0 & arr [j]> temp; j = j-increment) arr [j + increment] = arr [j]; arr [j + increment] = temp;} increment = increment/2;} void print (int arr [], int n) {int I; for (I = 0; I <n; I ++) {printf ("% d", arr [I]);} printf ("\ n");} int main () {int arr [] = {9, 1, 5, 8, 3, 7, 4, 6, 2}; int n = sizeof (arr) /sizeof (arr [0]); printf ("Before sorting: \ n"); print (arr, n); ShellSort (arr, n); printf ("after sorting: \ n "); print (arr, n); system (" pause "); return 0 ;}

 

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1282211

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.