-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert short.cpp
56 lines (51 loc) · 1.12 KB
/
insert short.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <conio.h>
#include <windows.h>
void setcolor (unsigned short color)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}
using namespace std;
int main(){
int y;
setcolor (10);
cout<<" /////////// " <<endl;
cout<<" || UTS || "<<endl;
cout<<"-------------------------"<<endl;
cout<<"||ALGORITMA INSERT SORT||"<<endl;
cout<<"-------------------------"<<endl;
cout<<endl;
setcolor (11);
cout<<"Masukan Jumlah Data = ";
cin>>y;
int x[y];
cout<<" "<<endl;
for(int i=1; i<y+1; i++){
setcolor (12);
cout<<"Masukan Data ke-"<<i<<"=";
cin>>x[i];
}
for(int i=1; i<y+1; i++){
int key = x[i];
int j = i-1;
while(j>=0 && x[j] > key){
x[j+1] = x[j];
j--;
}
x[j+1] = key;
cout<<" "<<endl;
setcolor (13);
cout<<"Hasil Putaran ke-"<<i<<"=";
for(int m=1;m<y+1;m++){
cout<<x[m]<<" ";
}
}
cout<<endl;
cout<<" "<<endl;
setcolor (14);
cout<<"Data setelah diurutkan="<<endl;
for(int m=1;m<y+1;m++){
cout<<x[m]<<" ";
}
}