-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shellSort_string.cpp
122 lines (108 loc) · 2.96 KB
/
shellSort_string.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//Yogi Arif Widodo creator deyawman.net scode.id yogi-aw.id
#include <iostream>
#include <Windows.h>
#include <string>
#include <conio.h>
using namespace std;
// void binarySort(string data[], int panjang); // tanpa ini juga bisa asalkan letak rekursi di atas program
void gotoxy(int x, int y)
{
COORD c = { x, y };
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE) , c);
}
//--
void createdbye()
{
cout <<"\n\nCreated By : ";
cout <<"TI 2A\n";
cout <<"\t\t\tYogi Arif Widodo ( NIM : 17615006 ) \n";
cout <<"\t\t\tShintya Pebrianti ( NIM : 17615008 ) ";
}
/* function to sort arr using shellSort */
void shellSort(string data[], int panjang)
{
// Start with a big gap, then reduce the gap
for (int gap = panjang/2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
// keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < panjang; i += 1)
{
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
string temp = data[i];
// shift earlier gap-sorted elements up until the correct
// location for a[i] is found
int j;
for (j = i; j >= gap && data[j - gap] > temp; j -= gap)
data[j] = data[j - gap];
// put temp (the original a[i]) in its correct location
data[j] = temp;
}
}
}
/*void binarySort(string data[], int panjang)
{
int i, j;
string selected;
string fw;
string mw;
string lw;
for(i=0; i < panjang - 1; i++)
{
for(j=i+1; j<panjang; j++)
{
fw = data[i].at(0);
lw = data[j].at(0);
if(fw<lw)
{
mw = data[i];
data[i] = data[j];
data[j] = mw;
}
}
}
}
*/
int main()
{
int i;
system("Color 0E");
string data[] = {
"Yogi Arif Widodo",
"Shintia Pebrianti",
"Cintya",
"Arnold Bold",
"Noer Sir D COL",
};
gotoxy(4,23);
createdbye();
gotoxy(4,1);
cout <<"\n\tProgram Binary Sort With Array Descending\n";
for(int y=0; y<66;y++)
{
cout <<"_";
}
cout <<endl <<"\nJumlah String : " ;
int str = sizeof(data) / sizeof(data[0]);
cout <<str <<"\n\n";
int panjang = sizeof(data) / sizeof(data[0]);
for (i = 0; i < panjang; i++) {
cout <<"\tInputan nama ke-"<<i+1 <<" = "<<data[i] << endl;
}
cout <<endl <<endl;
//-----------------------------------
shellSort(data, panjang);
//binarySort(data, panjang);
for ( int z=0;z <66;z++)
{
cout <<"_";
}
cout <<endl <<"\nHasil Pengurutan \n\n";
for (i = 0; i < panjang; i++) {
cout <<"\t" <<i+1 <<" = " << data[i] << endl;
}
_getch();
}