C language learning notes -- select sorting and insert sorting

Source: Internet
Author: User

# Include <stdio. h>



// Insert sorting,
Void sortArray (int p [], int n );
Void insert (int p [], int a, int B );

// Select sorting
Void selectedSort (int p [], int n );
Void swap (int & p1, int & p2 );

// Predefined
# Define ArrayCounts 9

Int main (){
Int a [] = {1, 3, 8, 2, 6, 5, 7, 4, 9 };
SortArray (a, ArrayCounts );
For (int I = 0; I <ArrayCounts; I ++ ){
Printf ("% d", a [I]);
}
Printf ("\ n ");
Int B [] = {1, 3, 8, 2, 6, 5, 7, 4, 9 };
SelectedSort (B, ArrayCounts );
For (int I = 0; I <ArrayCounts; I ++ ){
Printf ("% d", B [I]);
}
Printf ("\ n ");
Return 0;
}

Void sortArray (int p [], int n ){
For (int I = 1; I <n; I ++ ){
For (int j = 0; j <I; j ++ ){

// Locate the location to be inserted
If (p [I] <p [j])
Insert (p, j, I );
}
}
}

// Insert function implementation
Void insert (int p [], int a, int B ){
Int temp = p [B];
Printf ("temp: % d \ n", temp );
While (a <B ){
P [B] = p [b-1];
B --;
}
P [a] = temp;
}

Void selectedSort (int p [], int n ){
Int index;
For (int I = 0; I <n; I ++ ){

// Find the subscript of the minimum value each time
Index = I;
For (int j = I + 1; j <n; j ++ ){
If (p [index]> p [j])
Index = j;
}

// Place the minimum value at the beginning
Swap (p [I], p [index]);
}
}

Void swap (int & p1, int & p2 ){
Int temp = p1;
P1 = p2;
P2 = temp;
}

This article is from the "Xiaoyu 2013" blog. For more information, contact the author!

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.