Relive sorting algorithms

Source: Internet
Author: User

Because the teacher compared to the second-round interview more like to ask sorting algorithm that, so recently put the sorting algorithm review again, by the way write code.

1 Direct Insert sort.

#include <stdio.h>void inssort (int r[], int n) {int I, J, temp;for (i = 1; i < n; i++) {temp = R[i];j = I-1;while ( Temp < R[J])//Find the position with insert {r[j+1] = R[j];j = j-1;} R[J+1] = temp; Inserts a numeric value into the sorted queue}}void print (int r[], int n) {int i;for (i = 0; i < n; i++) printf ("%d", R[i]);p UTS ("\ n");} int main () {int a[10] = {3, 1, 5, 2, 7, 8, 9, 4, 6, 0};p rintf ("before sort:\n");p rint (A, ten), Inssort (A, ten);p rintf ("Sort AF ter:\n ");p rint (A, ten); return 0;}

 

As the above code: the TEMP variable is used to place the band insertion value.

The spatial complexity of the direct insertion algorithm O (1), because only 1 variables were used

The spatial complexity depends on the number of columns to be sorted. Best case Move Times 2 * (n-1), worst case Move (n+4) * (n-1)/2, average complexity n^2/4, so time complexity bit 0 (n^2)

Relive sorting algorithms

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.