Sort by Sorting Algorithm

Source: Internet
Author: User
1. Algorithm Description

Select sort: In an unordered array with a length of N, traverse n data in the first row, find the minimum value of which is exchanged with the first element, and traverse the remaining N-1 data in the second row, find the minimum value and switch it to the second element ...... the N-1 traverses the remaining 2 data, find out the smallest value exchange with the N-1 element, so now the selection is complete.

 

Ii. Algorithm Analysis

Average time complexity: O (n2)

Spatial complexity: O (1) (used for exchange and record indexing)

Stability: unstable (for example, in sequence [5, 5, 3], the first step is to exchange the first [5] with [3], resulting in the first 5 moving to the second 5)

3. Algorithm Implementation
 1 #include<stdio.h> 2 int main() 3 { 4     int array[] = {3, 1, 5, 2, 6 ,4}; 5     int count = sizeof(array) / sizeof(array[0]); 6     for (int i = 0; i < count - 1; i++) { 7         int minIndex = i; 8         for (int j = minIndex + 1; j < count; j++) { 9             if (array[minIndex] > array[j]) {10                 minIndex = j;11             }12         }13         if (minIndex != i) {14             int temp = 0;15             temp = array[minIndex];16             array[minIndex] = array[i];17             array[i] = temp;18         }19     }20     for (int i = 0; i < count; i++) {21         printf("%d\n", array[i]);22     }23     return 0;24 }

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.