2.1 Insertion sort
Insertion Sort
#includeusing namespace std;int main(){ int a[]={ 5,2,4,6,1,3}; int n=5; for (int j=1;j<=n;++j) { int key=a[j]; int i=j-1; while (( i >= 0)&& (a[i]>key)) { a[i+1]=a[i]; --i; } a[i+1]=key; } for (int k=0;k<=5;++k) cout< <
Exercise 2.1-2
Rewrite the INSERTION-SORT procedure to sort into nonincreasing instead of nondecreasing order.
#includeusing namespace std;int main(){ int a[]={ 5,2,4,6,1,3}; int n=5;for (int j=1;j<=n;++j) { int key=a[j]; int i=j-1; while (( i >= 0)&& (a[i]
Exercise 2.1-3
Linear Search
/****************************************************************************************Linear searchInput: A sequence of n numbers A=and a value v.Output: An index i such that v=A[i] or the special value NIL if v does not appear in A.****************************************************************************************/#include using namespace std;int main(){ int a[]={ 5,2,4,6,1,3}; int n=5; int v; cout<<"Please Enter a number: "<< endl; cin>>v; for (int i=0;i<=n;++i) { if (a[i]==v) cout<<"V=A["< <<"]"<