9.4.1 simple selection and sorting

Source: Internet
Author: User

Bubble Sorting is constantly compared. If it is small, it is exchanged. A simple sort is exchanged only after a loop is compared. The number of exchanges is less than that of Bubble sorting.

# 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 SelectSort (int arr [], int n) {int I; int j; int min; for (I = 0; I <n; I ++) {min = I; for (j = I + 1; j <n; j ++) {if (arr [min]> arr [j]) min = j;} if (I! = Min) swap (arr, I, min) ;}} 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); SelectSort (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/1282213

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.