forked from HPI-Artificial-Intelligence-Teaching/24-pt2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort.cpp
48 lines (40 loc) · 1.25 KB
/
insertion_sort.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
/******************************************************************************
*
* Sorts a sequence of strings from standard input using insertion sort
*
* Based on the source code from Robert Sedgewick and Kevin Wayne at https://algs4.cs.princeton.edu/
*
* % more tiny.txt
* S O R T E X A M P L E
*
* % ./insertion_sort < ../data/tiny.txt
* A E E L M O P R S T X [ one string per line ]
*
* % more words3.txt
* bed bug dad yes zoo ... all bad yet
*
* % ./insertion_sort < ../data/words3.txt
* all bad bed bug dad ... yes yet zoo [ one string per line ]
*
******************************************************************************/
#include <iostream>
#include <string>
#include "sort.h"
#define MAX_STR 100 // maximum number of strings
using namespace std;
int main(void) {
string val[MAX_STR];
int no_of_strings = 0;
// read the strings from standard input
while ((no_of_strings < MAX_STR) && (cin >> val [no_of_strings])) {
no_of_strings++;
}
// sort the strings
insertion_sort(val, no_of_strings);
// print the sorted strings
for (auto i = 0; i < no_of_strings; i++) {
cout << val[i] << " ";
}
cout << endl;
return 0;
}