Skip to content

Commit 96547db

Browse files
sorting shellSort cpp
just do it
1 parent ec199d8 commit 96547db

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

shellSort_string.cpp

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//Yogi Arif Widodo creator deyawman.net scode.id yogi-aw.id
2+
#include <iostream>
3+
#include <Windows.h>
4+
#include <string>
5+
#include <conio.h>
6+
7+
using namespace std;
8+
9+
// void binarySort(string data[], int panjang); // tanpa ini juga bisa asalkan letak rekursi di atas program
10+
void gotoxy(int x, int y)
11+
{
12+
COORD c = { x, y };
13+
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE) , c);
14+
15+
}
16+
//--
17+
void createdbye()
18+
{
19+
cout <<"\n\nCreated By : ";
20+
cout <<"TI 2A\n";
21+
cout <<"\t\t\tYogi Arif Widodo ( NIM : 17615006 ) \n";
22+
cout <<"\t\t\tShintya Pebrianti ( NIM : 17615008 ) ";
23+
}
24+
/* function to sort arr using shellSort */
25+
void shellSort(string data[], int panjang)
26+
{
27+
// Start with a big gap, then reduce the gap
28+
for (int gap = panjang/2; gap > 0; gap /= 2)
29+
{
30+
// Do a gapped insertion sort for this gap size.
31+
// The first gap elements a[0..gap-1] are already in gapped order
32+
// keep adding one more element until the entire array is
33+
// gap sorted
34+
for (int i = gap; i < panjang; i += 1)
35+
{
36+
// add a[i] to the elements that have been gap sorted
37+
// save a[i] in temp and make a hole at position i
38+
string temp = data[i];
39+
40+
// shift earlier gap-sorted elements up until the correct
41+
// location for a[i] is found
42+
int j;
43+
for (j = i; j >= gap && data[j - gap] > temp; j -= gap)
44+
data[j] = data[j - gap];
45+
46+
// put temp (the original a[i]) in its correct location
47+
data[j] = temp;
48+
}
49+
}
50+
51+
}
52+
/*void binarySort(string data[], int panjang)
53+
{
54+
int i, j;
55+
string selected;
56+
string fw;
57+
string mw;
58+
string lw;
59+
60+
for(i=0; i < panjang - 1; i++)
61+
{
62+
for(j=i+1; j<panjang; j++)
63+
{
64+
fw = data[i].at(0);
65+
lw = data[j].at(0);
66+
if(fw<lw)
67+
{
68+
mw = data[i];
69+
data[i] = data[j];
70+
data[j] = mw;
71+
}
72+
}
73+
}
74+
}
75+
*/
76+
int main()
77+
{
78+
int i;
79+
system("Color 0E");
80+
string data[] = {
81+
"Yogi Arif Widodo",
82+
"Shintia Pebrianti",
83+
"Cintya",
84+
"Arnold Bold",
85+
"Noer Sir D COL",
86+
87+
};
88+
gotoxy(4,23);
89+
createdbye();
90+
gotoxy(4,1);
91+
cout <<"\n\tProgram Binary Sort With Array Descending\n";
92+
for(int y=0; y<66;y++)
93+
{
94+
cout <<"_";
95+
}
96+
cout <<endl <<"\nJumlah String : " ;
97+
int str = sizeof(data) / sizeof(data[0]);
98+
cout <<str <<"\n\n";
99+
100+
int panjang = sizeof(data) / sizeof(data[0]);
101+
for (i = 0; i < panjang; i++) {
102+
103+
cout <<"\tInputan nama ke-"<<i+1 <<" = "<<data[i] << endl;
104+
}
105+
cout <<endl <<endl;
106+
107+
//-----------------------------------
108+
shellSort(data, panjang);
109+
//binarySort(data, panjang);
110+
for ( int z=0;z <66;z++)
111+
{
112+
cout <<"_";
113+
}
114+
cout <<endl <<"\nHasil Pengurutan \n\n";
115+
for (i = 0; i < panjang; i++) {
116+
117+
cout <<"\t" <<i+1 <<" = " << data[i] << endl;
118+
}
119+
_getch();
120+
121+
}
122+

0 commit comments

Comments
 (0)