Sort the direct Insert sort

Source: Internet
Author: User
Tags random seed

1. Principle: The array is divided into ordered and unordered 2 parts, generally with the first element as an ordered part, and then the unordered part (that is, the second element begins to the end of the data) the first element of the beginning and the ordered part of the order from a backward comparison, and then the unordered data inserted into the ordered data in 2. The direct insert sort is a stable sort, and the basic operation is to insert a record into an ordered table that is already sorted, resulting in a new, sequential table with a 1 increase in the number of records. The time complexity is O (n*n) and the auxiliary space is O (1) 3. Sort Demo
4.java Implementation method
public class Straightinsertionsort {/** * Helper function: Output array Contents * * @param A: To output array */public static void PrintArray (int[] a) {for (i NT index = 0; Index < a.length; index++) {System.out.print (A[index] + "");} System.out.println ("");} /** * Direct Insert Sort Implementation 1 * * @param a: Array to be sorted */public static void Insertsort (int[] a) {//Output original array printarray (a); for (int index = 1; Index < a.length; index++) {int subindex = index;//wait for data to be inserted int currentdata = A[index];while ((subindex > 0) && (a[subindex-1] & Gt CurrentData)) {A[subindex] = a[subindex-1];subindex--;} Insert into the appropriate position a[subindex] = currentdata;//output after each sort PrintArray (a);}} /** * Direct Insert Sort Implementation 2 * @param A: Array to be sorted */public static void InsertSort2 (int[] a) {int n = a.length;int I, j;for (i = 0; i < N i++) {int temp = a[i];for (j = i; j > 0 && Temp < a[j-1]; j--) {a[j] = a[j-1];} A[J] = Temp;printarray (a);}} public static void Main (string[] args) {//insertsort (new int[] {8, 2, 4, 9, 3, 6, 7, Ten}) InsertSort2 (new int[] {8, 2, 4, 9, 3, 6, 7, 10});}} 

How to implement 5.C language
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #define MAXSIZE 10typedef struct {int length; int arr[maxsize+2];} List;void Insert (list* p) {int i,j; for (i = 2; I <= p->length; i + = 1)//first staged, the second number is the number to be compared, starting from the third comparison {P->arr [0] = The first digit of the p->arr[i];//array is used to stage each arr[i] For comparison (j = i-1; (P->arr[0] < p->arr[j]) && j!=0;     J--) {p->arr[j+1] = p->arr[j];  } P->arr[j+1] = p->arr[0]; }}int Main () {int i; list *plist; plist = (list*) malloc (sizeof (list));//Allocation space plist->length = MAXSIZE; Plist->arr[max SIZE+2] = 0;//The last digit is set to 0 srand ((unsigned int) time (NULL));//random seed generation for (i = 1; i < maxsize+1; i + 1)//The first digit of the array is used to stage the  Arr[i] {Plist->arr[i] = (rand ()% 100);//generate 10 x 100 or less random number printf ("Array before:%d\n", plist->arr[i]); } insert (plist); printf ("\ n");  for (i = 1; i < maxsize+1; i + = 1) {printf ("array after:%d\n", plist->arr[i]); } return 0;



Sort the direct Insert 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.