Introduction to Algorithms 2.1 Insert sort

Source: Internet
Author: User

Insert Sort

Insertion_sort.h not with Template#include <iostream> #include <stdint.h>//insertion-sort (A)//For j = 2 to  a.length//key = a[j]////Insert a[j] into sorted sequence a[1..j-1].//i = j-1//while i > 0 and A[i] > key//a[i + 1] = a[i]//i = i-1//a[i + 1] = key//allow the same keyvoid Insertion_sort (    uint64_t* A, uint64_t const n) {uint64_t key;    int64_t i;        j = 2 to A.length (J 1 to N-1) for (uint64_t j = 1; j < N; j + +)//J begin with A[1] {key = A[j];                   i = j-1;            I begin with a[0] while (i >= 0 && a[i] > key) {a[i + 1] = A[i];        i--;    } a[i + 1] = key; }}//insertion_sort.cpp#include "insertion_sort.h" #ifdef __linux#include <stdio.h> #endifint Main () {uint64_t    Array[6] = {5, 2, 4, 6, 1, 3}; for (uint64_t i = 0; i < sizeof (array)/sizeof (uint64_t); i++) {std::cout << Array[i] << "";    } std::cout << Std::endl;    Insertion_sort (array, sizeof (array)/sizeof (uint64_t));    for (uint64_t i = 0; i < sizeof (array)/sizeof (uint64_t); i++) {std::cout << array[i] << "";    } std::cout << Std::endl;    GetChar (); return 0;}

Inserting a sort template

Insert sort insertion_sort_t.h#include <iostream> #include <string>//insertion-sort (A)//For j = 2 to a.length// Key = a[j]////Insert a[j] into sorted sequence a[1..j-1].//i = j-1//while i > 0 and a[i] > ke y//a[i + 1] = a[i]//i = i-1//a[i + 1] = keytemplate <class type> void insert_sort_t (Type *    const A, int const & N) {Type key;     J is assigned a value of 1 because the second element starts with the insertion sort//J<n because n represents the number of array elements to be sorted, n-1 is the last element for (int j = 1; j < N; j + +) {key = A[j]; The element waiting to be inserted is a[j] int i = j-1;  A[0...j-1] As an already ordered part, A[j+1...n-1] is not a sorted part//We first want to compare a[j] with a[j-1] while ((i >= 0) && (A[i] >        Key) {a[i + 1] = A[i];//all elements larger than a[j] move back one i--;      } a[i + 1] = key; Put A[j] in the right place}}//insertion_sort_t.cpp#include "insertion_sort_t.h" #ifdef __linux#include <stdio.h>#    Endifint Main () {int a[6] = {5, 2, 4, 6, 1, 3}; Insert_sort_t (A,6);    for (int i = 0; i < 6; i++) {std::cout << a[i] << "";    } std::cout << Std::endl;    std::string B[4] = {"Hello", "China", "haha", "I Love China"};    insert_sort_t (b, 4);    for (int i = 0; i < 4; i++) {std::cout << b[i] << "";    } std::cout << Std::endl;    GetChar (); return 0;}

Introduction to the

Algorithm 2.1 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.