#!/usr/local/python35/bin/python3.5## # # Insert Sortif __name__=="__main__": Var_list=[3,2,4,5,1] """from the second item, start by comparing it to the item in front of it, and if it is smaller than the previous item, go to the front. """ forIndexinchRange (1, Len (var_list)): Key=var_list[index]## Record The value of the second itemI=index-1## Record the subscript of the preceding paragraph whileI>=0 andKey<var_list[i]:## If the bar is satisfied, move the preceding paragraph backwards and move the subscript forward by one of the precedingvar_list[i+1]=Var_list[i] I=i-1Var_list[i+1]=key## Write back Print(var_list)
#include <iostream>usingstd::cout;usingstd::cin;usingStd::endl;intMain () {intvar_list[5]={3,2,4,5,1}; for(intI=1; i<=4; i++) { intkey=Var_list[i]; intj=i-1; while(j>=0&& var_list[j]>key) {Var_list[j+1]=Var_list[j]; J=j-1; } var_list[j+1]=key; } for(intI=0;i<5; i++) {cout<<var_list[i]<<","; } cout<<Endl;}
Introduction to Algorithms--python--Insert sort