Sort by direct sort (edge generation edge Sort)

Source: Internet
Author: User

First, the principle:

Suppose the array is numarr[4], and the first element of the array has been assigned a value of 3,numarr[0] = 3;

1. Suppose that a number of temp = 5 is generated, to be sorted when inserting an array, the comparison is preceded by numarr[0], temp > numarr[0]; Then 5 should be inserted after 3, i.e. numarr[1] = 5;

2. If temp = 1,temp < numarr[0]; Then 1 should be inserted before 3, while the 3 and later elements will be one backwards, i.e.

for (int i = 4; i > 0; i--) {

Numarr[i] = numarr[i-1];

}

Numarr[0] = 1;

3. If numarr[0] = 3; NUMARR[1] = 5; to insert temp = 3, before 1 comparison, temp > Numarr[0], no action. Compared with 5, temp < numarr[1], the elements of number 2nd and beyond will be shifted one bit backwards and the value of temp to numarr[1], i.e.:

for (int i = 4; i > 1; i--) {

Numarr[i] = numarr[i-1];

}

NUMARR[1] = temp;

Second, expand: delete operation

Delete the second element of the above array, then the loop I should go after, that is:

for (int i = 1; I <3; i + +) {

Numarr[i] = numarr[i + 1];

}

NUMARR[4] = 0;

Third, the full source:

1#include <stdio.h>2#include <stdbool.h>3 4 intMainintargcConst Char*argv[]) {5     6     intstunum[Ten] = {1,2,3,4,5,6,7};7     8      for(inti =0; I <Ten; i + +) {9printf"%d", Stunum[i]);Ten     } Oneprintf"\ n"); A      -      -      the     //Insert an element 20 before the fourth element, from the back forward -     -      for(inti =9; i >3; I--) { -Stunum[i] = stunum[i-1]; +     } -stunum[3] = -; +      A      for(inti =0; I <Ten; i + +) { atprintf"%d", Stunum[i]); -     } -      -      -     //Delete the value of the second element, once in the future -      for(inti =1; I <Ten; i + +) { inStunum [i] = stunum [i +1]; -     } tostunum[9] =0; +      -printf"\ n"); the      for(inti =0; I <Ten; i + +) { *printf"%d", Stunum[i]); $     }Panax Notoginseng      -      the     return 0; +      A}

Sort directly (edge-generated edge sort)

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.