Anda di halaman 1dari 14

PROGRAM MENU MAKANAN

1. #include <iostream>
2. #include <stdio.h>
3. #include <stdlib.h>
4. #include <conio.h>
5.
6. using namespace std;
7.
8. struct data_mahasiswa
9. {
10. string namapelanggan;
11. string nama[100];
12. double price[100];
13. };
14.
15. int main()
16. {
17. string namapelanggan;
18. cout << "Nama pelanggan : " << " ";
19. cin >> namapelanggan;
20.
21. struct menuItemType{
22. int jumlah;
23. char nama[100];
24. double harga;
25. };
26.
27. menuItemType menuList[100];
28.
29. //Input struct data menu
30. cout<<" Tulis menu : \n";
31. cout<<"-----------------------\n";
32. int a=0;
33. for(int i=0;i<100;i++){
34.
35. cout<<" Nama menu : "; cin>>menuList[i].nama; cout<<endl;
36. cout<<" Jumlah : "; cin>>menuList[i].jumlah; cout<<endl;
37. cout<<" Harga (Rp) : "; cin>>menuList[i].harga;
38. cout<<"-----------------------\n";
39.
40. a+=i;
41.
42. cout<<"Pilih menu lain? (1 = yes/ 0 = no): ";
43. int jawab;
44. cin>>jawab;
45.
46. if (jawab==0)
47. break;
48. }
49. cout<<"-----------------------\n";
50.
51. double sum=0;
52. for(int i=0;i<=a;i++){
53. sum=0;
54. sum=sum+menuList[i].jumlah;
55. cout<<"Total "<<menuList[i].nama<<" : "<<sum<<endl<<endl;
56. }
57.
58. cout<<"-----------------------\n";
59.
60. double total = 0;
61. double x=0;
62. double potongan=0;
63. cout<<"Total Harga : ";
64. for(int i=0;i<=a;i++){
65.
66. if (menuList[i].jumlah>=5){
67. potongan=(menuList[i].harga*menuList[i].jumlah)*0.1;
68. x=(menuList[i].harga*menuList[i].jumlah)-potongan;
69.
70. }
71. else {
72. x=(menuList[i].harga*menuList[i].jumlah);
73. }
74. total = total+x;
75. }
76. cout<<"Rp "<<total;
77. }

FUNGSI REKURSIF DERET


1. #include <iostream>
2. #include <conio.h>
3. using namespace std;
4.
5.
6.
7. long deret(int n)
8.
9. {
10. if (n==1)
11. return 3;
12.
13. else
14. return deret(n-1)+((n-1)*2);
15.
16. }
17.
18. int main()
19.
20. {
21.
22. int x;
23. int y;
24. cout<<"Masukan jumlah suku deret (N) : ";
25.
26. cin>>x;
27.
28. for (y=1;y<=x;y++)
29. cout<<"N ke-" << y <<" = " << deret(y) <<endl;
30.
31.
32.
33. return 0;
34.
35. }

RATA-RATA

1. // MUHAMMAD DHAFIN RAHMATUKA BASYSYASY SUKMAWAN


2. //19/442432/PA/19181
3. #include <iostream>
4. using namespace std;
5.
6. int main(){
7. int data[100];
8. int n,i;
9.
10. cout<<"Masukan jumlah data: ";
11. cin>>n;
12.
13. for(i=1;i<=n;i++){
14. cout<<"Masukkan nilai ke - "<<i<< " : ";
15. cin>>data[i];
16. }
17.
18. int jumlah=0;
19. for(int i=1; i<=n; i++){
20. jumlah = jumlah + data[i];
21. }
22. cout<<"Total: "<<jumlah<<endl;
23.
24. int average;
25. average=jumlah/n;
26. cout<<"Rata-rata: "<<average;
27.
28. return 0;
29. }

FIBONACCI

1. // MUHAMMAD DHAFIN RAHMATUKA BASYSYASY SUKMAWAN


2. //19/442432/PA/19181
3. #include <iostream>
4. using namespace std;
5.
6. int fibonacci(int n) {
7. if (n == 0 || n ==1){
8. return n=1;
9. }
10. else {
11. return (fibonacci(n-1) + fibonacci(n-2));
12. }
13. }
14.
15. int main() {
16. int n, i, j = 0;
17. int maks;
18. cout << "Masukkan maksimal jumlah bilangan fibonacci: ";
19. cin >> maks;
20.
21. int jumlah=0;
22. for (int j=0; jumlah<=maks;j++){
23. jumlah=jumlah+fibonacci(j);
24. n=j;
25. }
26.
27. cout << "Hasil bilangan fibonacci: \n";
28. for (i = 1; i <= n;i++){
29. cout << "fibonacci ke-"<< i <<": " << fibonacci(j)<<endl;
30. j++;
31. }
32. cout<< "jumlah:"<<jumlah - fibonacci(j) <<endl;
33. cout<< "banyak elemen:"<<n;
34. return 0;
35. }

TUGAS JARAK HARI

1. #include <iostream>
2. using namespace std;
3.
4. struct Date
5. {
6. int year;
7. int month;
8. int day;
9. };
10. // KABISAT
11. bool isLeap(int year)
12. {
13. if(year%4==0 && year%100==0 && year%400==0 || year%4==0 && year%100!=0)
14. return true;
15. else
16. return false;
17. }
18. // JUMLAH HARI BULAN
19. int monthLength(int year, int month)
20. {
21. if (month==2)
22. {
23. if(isLeap (year)==1)
24. return 29;
25. else
26. return 28;
27. }
28. else if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
29. return 31;
30. else
31. return 30;
32. }
33.
34. int dayOfYear(Date date)
35. {
36. int x=0;
37. for (int i=1; i<=date.month - 1; i++)
38. {
39. x+=monthLength(date.year, i);
40. }
41. return x+date.day;
42. }
43. int daysBetween(Date d1, Date d2)
44. {
45. int total=0;
46. if (d1.year!=d2.year)
47. {
48. for(int i=d1.year; i<=d2.year - 1; i++)
49. {
50. if(d1.year==d2.year)
51. {
52. for (int j=d1.month; j<d2.month; j++)
53. {
54. total+=monthLength(d1.year,j);
55. }
56. } else
57. {
58. if(isLeap(d1.year+i)==1)
59. {
60. total+=366;
61. } else
62. total+=365;
63. }
64. }
65. } else
66. {
67. if(d2.month>d1.month)
68. {
69. for(int j=d1.month; j<d2.month; j++)
70. {
71. total+=monthLength(d1.year, j);
72. }
73. } else
74. {
75. for(int j=d2.month; j<d1.month; j++)
76. {
77. total-=monthLength(d1.year, j);
78. }
79. }
80. }
81. return total+(d2.day-d1.day);
82. }
83. int main(void)
84. {
85. Date since, till;
86. cout<<"Enter first date (y m d):";
87. cin>>since.year>>since.month>>since.day;
88. cout<<"Enter second date (y m d):";
89. cin>>till.year>>till.month>>till.day;
90. cout<<daysBetween(since,till)<<endl;
91. return 0;
92. }

SORTING
1. #include <iostream>
2. #include <time.h>
3. #include <stdio.h>
4. #include <conio.h>
5. #include <fstream>
6. using namespace std;
7.
8. void quickSort(int *Data,int a,int b);
9. void merge(int *a, int low, int high, int mid);
10. void mergeSort(int *a, int low, int high);
11.
12. int main(){
13. clock_t start;
14. double duration;
15.
16. int data1[100],data2[1000],data3[10000];
17.
18. //100
19. //insertion sort
20. cout<<"Untuk array sejumlah 100:\n";
21. start=clock();
22. int baris=0;
23. cout<<"#Insertion Sort#\n";
24. cout<<"Before:\n";
25. for(int i=0;i<100;i++){
26. data1[i]=100-i;
27. cout<<data1[i]<<" ";
28. baris++;
29. if(baris==10){
30. cout<<endl;
31. baris=0;
32. }
33. }
34. cout<<endl;
35. int temp;
36. for(int j=1;j<100;j++){
37. int i=j-1;
38. temp=data1[j];
39. while(data1[i]>temp&&i>=0){
40. data1[i+1]=data1[i];
41. i--;
42. }
43. data1[i+1]=temp;
44. }
45. baris=0;
46. cout<<"After:\n";
47. for(int i=0;i<100;i++){
48. cout<<data1[i]<<" ";
49. baris++;
50. if(baris==10){
51. cout<<endl;
52. baris=0;
53. }
54. }
55. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
56. float i_t100 = duration;
57. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
58.
59. //selection sort
60. start=clock();
61. cout<<endl<<endl<<endl;
62. cout<<"#Selection Sort#\n";
63. cout<<"Before:\n";
64. for(int i=0;i<100;i++){
65. data1[i]=100-i;
66. cout<<data1[i]<<" ";
67. baris++;
68. if(baris==10){
69. cout<<endl;
70. baris=0;
71. }
72. }
73. cout<<endl;
74. int min;
75. for(int i=0;i<100-1;i++){
76. min=i;
77. for(int j=i+1;j<100;j++){
78. if(data1[j]<data1[min]){
79. min=j;
80. }
81. }
82. temp=data1[i];
83. data1[i]=data1[min];
84. data1[min]=temp;
85. }
86. baris=0;
87. cout<<"After:\n";
88. for(int i=0;i<100;i++){
89. cout<<data1[i]<<" ";
90. baris++;
91. if(baris==10){
92. cout<<endl;
93. baris=0;
94. }
95. }
96. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
97. float s_t100 = duration;
98. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
99.
100.
101. //bubble sort
102. start=clock();
103. cout<<endl<<endl<<endl;
104. cout<<"#Bubble Sort#\n";
105. cout<<"Before:\n";
106. for(int i=0;i<100;i++){
107. data1[i]=100-i;
108. cout<<data1[i]<<" ";
109. baris++;
110. if(baris==10){
111. cout<<endl;
112. baris=0;
113. }
114. }
115. cout<<endl;
116. for(int i=0;i<100-1;i++){
117. for(int j=0;j<100-1;j++){
118. if(data1[j]>data1[j+1]){
119. temp=data1[j];
120. data1[j]=data1[j+1];
121. data1[j+1]=temp;
122. }
123. }
124. }
125. baris=0;
126. cout<<"After:\n";
127. for(int i=0;i<100;i++){
128. cout<<data1[i]<<" ";
129. baris++;
130. if(baris==10){
131. cout<<endl;
132. baris=0;
133. }
134. }
135. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
136. float b_t100 = duration;
137. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
138.
139.
140. //merge sort
141. start=clock();
142. cout<<endl<<endl<<endl;
143. cout<<"#Merge Sort#\n";
144. cout<<"Before:\n";
145. for(int i=0;i<100;i++){
146. data1[i]=100-i;
147. cout<<data1[i]<<" ";
148. baris++;
149. if(baris==10){
150. cout<<endl;
151. baris=0;
152. }
153. }
154. cout<<endl;
155. mergeSort(data1,0,99);
156. baris=0;
157. cout<<"After:\n";
158. for(int i=0;i<100;i++){
159. cout<<data1[i]<<" ";
160. baris++;
161. if(baris==10){
162. cout<<endl;
163. baris=0;
164. }
165. }
166. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
167. float ms_t100 = duration;
168. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
169.
170. //quick sort
171. start=clock();
172. cout<<endl<<endl<<endl;
173. cout<<"#Quick Sort#\n";
174. cout<<"Before:\n";
175. for(int i=0;i<100;i++){
176. data1[i]=100-i;
177. cout<<data1[i]<<" ";
178. baris++;
179. if(baris==10){
180. cout<<endl;
181. baris=0;
182. }
183. }
184. quickSort(data1,0,99);
185. baris=0;
186. cout<<endl;
187. cout<<"After:\n";
188. for(int i=0;i<100;i++){
189. cout<<data1[i]<<" ";
190. baris++;
191. if(baris==10){
192. cout<<endl;
193. baris=0;
194. }
195. }
196. cout<<endl;
197. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
198. float qs_t100 = duration;
199. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
200.
201. //1000
202. //insertion sort
203. cout<<endl<<endl<<endl;
204. cout<<"Untuk array sejumlah 1000:\n";
205. start=clock();
206. baris=0;
207. cout<<"#Insertion Sort#\n";
208. cout<<"Before:\n";
209. for(int i=0;i<1000;i++){
210. data2[i]=1000-i;
211. cout<<data2[i]<<" ";
212. baris++;
213. if(baris==10){
214. cout<<endl;
215. baris=0;
216. }
217. }
218. cout<<endl;
219. temp;
220. for(int j=1;j<1000;j++){
221. int i=j-1;
222. temp=data2[j];
223. while(data2[i]>temp&&i>=0){
224. data2[i+1]=data2[i];
225. i--;
226. }
227. data2[i+1]=temp;
228. }
229. baris=0;
230. cout<<"After:\n";
231. for(int i=0;i<1000;i++){
232. cout<<data2[i]<<" ";
233. baris++;
234. if(baris==10){
235. cout<<endl;
236. baris=0;
237. }
238. }
239. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
240. float i_t1000 = duration;
241. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
242.
243. //selection sort
244. start=clock();
245. cout<<endl<<endl<<endl;
246. cout<<"#Selection Sort#\n";
247. cout<<"Before:\n";
248. for(int i=0;i<1000;i++){
249. data2[i]=1000-i;
250. cout<<data2[i]<<" ";
251. baris++;
252. if(baris==10){
253. cout<<endl;
254. baris=0;
255. }
256. }
257. cout<<endl;
258. min;
259. for(int i=0;i<1000-1;i++){
260. min=i;
261. for(int j=i+1;j<1000;j++){
262. if(data2[j]<data2[min]){
263. min=j;
264. }
265. }
266. temp=data2[i];
267. data2[i]=data2[min];
268. data2[min]=temp;
269. }
270. baris=0;
271. cout<<"After:\n";
272. for(int i=0;i<1000;i++){
273. cout<<data2[i]<<" ";
274. baris++;
275. if(baris==10){
276. cout<<endl;
277. baris=0;
278. }
279. }
280. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
281. float s_t1000 = duration;
282. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
283.
284.
285. //bubble sort
286. start=clock();
287. cout<<endl<<endl<<endl;
288. cout<<"#Bubble Sort#\n";
289. cout<<"Before:\n";
290. for(int i=0;i<1000;i++){
291. data2[i]=1000-i;
292. cout<<data2[i]<<" ";
293. baris++;
294. if(baris==10){
295. cout<<endl;
296. baris=0;
297. }
298. }
299. cout<<endl;
300. for(int i=0;i<1000-1;i++){
301. for(int j=0;j<1000-1;j++){
302. if(data2[j]>data2[j+1]){
303. temp=data2[j];
304. data2[j]=data2[j+1];
305. data2[j+1]=temp;
306. }
307. }
308. }
309. baris=0;
310. cout<<"After:\n";
311. for(int i=0;i<1000;i++){
312. cout<<data2[i]<<" ";
313. baris++;
314. if(baris==10){
315. cout<<endl;
316. baris=0;
317. }
318. }
319. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
320. float b_t1000 = duration;
321. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
322.
323.
324. //merge sort
325. start=clock();
326. cout<<endl<<endl<<endl;
327. cout<<"#Merge Sort#\n";
328. cout<<"Before:\n";
329. for(int i=0;i<1000;i++){
330. data2[i]=1000-i;
331. cout<<data2[i]<<" ";
332. baris++;
333. if(baris==10){
334. cout<<endl;
335. baris=0;
336. }
337. }
338. cout<<endl;
339. mergeSort(data2,0,999);
340. baris=0;
341. cout<<"After:\n";
342. for(int i=0;i<1000;i++){
343. cout<<data2[i]<<" ";
344. baris++;
345. if(baris==10){
346. cout<<endl;
347. baris=0;
348. }
349. }
350. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
351. float ms_t1000 = duration;
352. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
353.
354. //quick sort
355. start=clock();
356. cout<<endl<<endl<<endl;
357. cout<<"#Quick Sort#\n";
358. cout<<"Before:\n";
359. for(int i=0;i<1000;i++){
360. data2[i]=1000-i;
361. cout<<data2[i]<<" ";
362. baris++;
363. if(baris==10){
364. cout<<endl;
365. baris=0;
366. }
367. }
368. quickSort(data2,0,999);
369. baris=0;
370. cout<<endl;
371. cout<<"After:\n";
372. for(int i=0;i<1000;i++){
373. cout<<data2[i]<<" ";
374. baris++;
375. if(baris==10){
376. cout<<endl;
377. baris=0;
378. }
379. }
380. cout<<endl;
381. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
382. float qs_t1000 = duration;
383. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
384.
385. //1000
386. //insertion sort
387. cout<<endl<<endl<<endl;
388. cout<<"Untuk array sejumlah 1000:\n";
389. start=clock();
390. baris=0;
391. cout<<"#Insertion Sort#\n";
392. cout<<"Before:\n";
393. for(int i=0;i<10000;i++){
394. data3[i]=10000-i;
395. cout<<data3[i]<<" ";
396. baris++;
397. if(baris==10){
398. cout<<endl;
399. baris=0;
400. }
401. }
402. cout<<endl;
403. temp;
404. for(int j=1;j<10000;j++){
405. int i=j-1;
406. temp=data3[j];
407. while(data3[i]>temp&&i>=0){
408. data3[i+1]=data3[i];
409. i--;
410. }
411. data3[i+1]=temp;
412. }
413. baris=0;
414. cout<<"After:\n";
415. for(int i=0;i<10000;i++){
416. cout<<data3[i]<<" ";
417. baris++;
418. if(baris==10){
419. cout<<endl;
420. baris=0;
421. }
422. }
423. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
424. float i_t10000 = duration;
425. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
426.
427. //selection sort
428. start=clock();
429. cout<<endl<<endl<<endl;
430. cout<<"#Selection Sort#\n";
431. cout<<"Before:\n";
432. for(int i=0;i<10000;i++){
433. data2[i]=10000-i;
434. cout<<data3[i]<<" ";
435. baris++;
436. if(baris==10){
437. cout<<endl;
438. baris=0;
439. }
440. }
441. cout<<endl;
442. min;
443. for(int i=0;i<10000-1;i++){
444. min=i;
445. for(int j=i+1;j<10000;j++){
446. if(data3[j]<data3[min]){
447. min=j;
448. }
449. }
450. temp=data3[i];
451. data3[i]=data3[min];
452. data3[min]=temp;
453. }
454. baris=0;
455. cout<<"After:\n";
456. for(int i=0;i<10000;i++){
457. cout<<data3[i]<<" ";
458. baris++;
459. if(baris==10){
460. cout<<endl;
461. baris=0;
462. }
463. }
464. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
465. float s_t10000 = duration;
466. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
467.
468.
469. //bubble sort
470. start=clock();
471. cout<<endl<<endl<<endl;
472. cout<<"#Bubble Sort#\n";
473. cout<<"Before:\n";
474. for(int i=0;i<10000;i++){
475. data3[i]=10000-i;
476. cout<<data3[i]<<" ";
477. baris++;
478. if(baris==10){
479. cout<<endl;
480. baris=0;
481. }
482. }
483. //cout<<endl;
484. for(int i=0;i<10000-1;i++){
485. for(int j=0;j<10000-1;j++){
486. if(data3[j]>data3[j+1]){
487. temp=data3[j];
488. data3[j]=data3[j+1];
489. data3[j+1]=temp;
490. }
491. }
492. }
493. baris=0;
494. cout<<"After:\n";
495. for(int i=0;i<10000;i++){
496. cout<<data3[i]<<" ";
497. baris++;
498. if(baris==10){
499. cout<<endl;
500. baris=0;
501. }
502. }
503. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
504. float b_t10000 = duration;
505. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
506.
507.
508. //merge sort
509. start=clock();
510. cout<<endl<<endl<<endl;
511. cout<<"#Merge Sort#\n";
512. cout<<"Before:\n";
513. for(int i=0;i<10000;i++){
514. data3[i]=10000-i;
515. cout<<data3[i]<<" ";
516. baris++;
517. if(baris==10){
518. cout<<endl;
519. baris=0;
520. }
521. }
522. cout<<endl;
523. mergeSort(data3,0,9999);
524. baris=0;
525. cout<<"After:\n";
526. for(int i=0;i<10000;i++){
527. cout<<data3[i]<<" ";
528. baris++;
529. if(baris==10){
530. cout<<endl;
531. baris=0;
532. }
533. }
534. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
535. float ms_t10000 = duration;
536. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
537.
538. //quick sort
539. start=clock();
540. cout<<endl<<endl<<endl;
541. cout<<"#Quick Sort#\n";
542. cout<<"Before:\n";
543. for(int i=0;i<10000;i++){
544. data3[i]=10000-i;
545. cout<<data3[i]<<" ";
546. baris++;
547. if(baris==10){
548. cout<<endl;
549. baris=0;
550. }
551. }
552. quickSort(data3,0,9999);
553. baris=0;
554. cout<<endl;
555. cout<<"After:\n";
556. for(int i=0;i<10000;i++){
557. cout<<data3[i]<<" ";
558. baris++;
559. if(baris==10){
560. cout<<endl;
561. baris=0;
562. }
563. }
564. cout<<endl;
565. duration = (clock() - start) / (double) CLOCKS_PER_SEC;
566. float qs_t10000 = duration;
567. cout<<"Waktu yang dibutuhkan: "<< duration <<' detik.\n';
568.
569. cout<<endl<<endl<<endl;
570. cout<<"Untuk array sejumlah 100, kecepatan masing-masing algoritma:\n";
571. cout<<"Insertion Sort : "<<i_t100<<endl;
572. cout<<"Selection Sort : "<<s_t100<<endl;
573. cout<<"Bubble Sort : "<<b_t100<<endl;
574. cout<<"Merge Sort : "<<ms_t100<<endl;
575. cout<<"Quick Sort : "<<qs_t100<<endl;
576.
577. cout<<endl;
578.
579. cout<<"Untuk array sejumlah 1000, kecepatan masing-masing algoritma:\n";
580. cout<<"Insertion Sort : "<<i_t1000<<endl;
581. cout<<"Selection Sort : "<<s_t1000<<endl;
582. cout<<"Bubble Sort : "<<b_t1000<<endl;
583. cout<<"Merge Sort : "<<ms_t1000<<endl;
584. cout<<"Quick Sort : "<<qs_t1000<<endl;
585.
586. cout<<endl;
587.
588. cout<<"Untuk array sejumlah 10000, kecepatan masing-masing algoritma:\n";
589. cout<<"Insertion Sort : "<<i_t10000<<endl;
590. cout<<"Selection Sort : "<<s_t10000<<endl;
591. cout<<"Bubble Sort : "<<b_t10000<<endl;
592. cout<<"Merge Sort : "<<ms_t10000<<endl;
593. cout<<"Quick Sort : "<<qs_t10000<<endl;
594.
595. return 0;
596. }
597.
598. void quickSort(int *Data,int a,int b){
599. int a1,b1,pivot;
600. a1=a;b1=b;
601. pivot=Data[(a+b)/2];
602. while(!(a1>b1)){
603. while(Data[a1]<pivot) a1++;
604. while(Data[b1]>pivot) b1--;
605. if(a1<=b1){
606. swap(Data[a1],Data[b1]);
607. a1++; b1--;
608. }
609. }
610. if(a<b1) quickSort(Data,a,b1);
611. if(a1<b) quickSort(Data,a1,b);
612. }
613.
614. void merge(int *a,int low,int high,int mid){
615. int i,j,k,temp[high-low+1];
616. i=low;
617. k=0;
618. j=mid+1;
619. while (i<=mid&&j<=high){
620. if (a[i]<a[j]){
621. temp[k]=a[i];
622. k++;
623. i++;
624. }
625. else{
626. temp[k]=a[j];
627. k++;
628. j++;
629. }
630. }
631. while(i<=mid){
632. temp[k]=a[i];
633. k++;
634. i++;
635. }
636. while(j<=high){
637. temp[k]=a[j];
638. k++;
639. j++;
640. }
641. for(i=low;i<=high;i++){
642. a[i]=temp[i-low];
643. }
644. }
645.
646. void mergeSort(int *a,int low,int high){
647. int mid;
648. if (low<high){
649. mid=(low+high)/2;
650. mergeSort(a,low,mid);
651. mergeSort(a,mid+1,high);
652. merge(a,low,high,mid);
653. }
654. }

Anda mungkin juga menyukai