University of Leeds

Sorting Data

Introduction

If your program has some data, a common task is to do some sorting. Luckily the C++ standard template library contains some useful functions to make this simple.

Example

The following example writes some random values to a text file. It then reads the values in the text file into an array. The values are then sorted and printed to a new file.

main.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
122
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <string>

// function prototypes
void write_random_number_file();
int get_random_number(int low, int high);
int count_lines();
void read_into_array(int array[], int n);
void order_array(int array[], int n);
void write_array_to_file(int array[], int n);

int main() {
  // write random numbers to file 'random.txt'
  write_random_number_file();
  // count the lines and create array of correct size
  int n = count_lines();
  int *array = new int[n];
  // read the random numbers into the array
  read_into_array(array, n);
  // order the values in the array
  order_array(array, n);
  // then write ordered values to file 'ordered.txt'
  write_array_to_file(array, n);
}

void write_random_number_file() {
  // create an output file stream
  std::ofstream output;
  // use it to open a file named 'output.txt'
  output.open("random.txt");
  // check if the file is not open
  if (!output.is_open()) {
    // print error message and quit if a problem occurred
    std::cerr << "Error creating file!\n";
    exit(1);
  }
  // write 100 random numbers in range 0 to 100 to the file
  for (int i = 0; i < 100; i++) {
    int number = get_random_number(0, 100);
    output << number << std::endl;
  }
  output.close();
}

// get random number in range
int get_random_number(int low, int high) {
  int seed =
      std::chrono::high_resolution_clock::now().time_since_epoch().count();
  std::mt19937 generator(seed);
  std::uniform_int_distribution<int> distribution(low, high);
  return distribution(generator);
}

int count_lines() {
  // create an input file stream
  std::ifstream input;
  // use it to open a file named 'random.txt'
  input.open("random.txt");
  // check if the file is not open
  if (!input.is_open()) {
    // print error message and quit if a problem occurred
    std::cerr << "Error! No input file found!\n";
    exit(1);
  }
  int n = 0;
  std::string dummy;
  // keep reading lines in file until no lines left to read
  // read into dummy string and increment count
  while (getline(input, dummy)) {
    n++;
  }
  input.close();
  return n;
}

void read_into_array(int array[], int n) {
  // create an input file stream
  std::ifstream input;
  // use it to open a file named 'MOCK_DATA.csv'
  input.open("random.txt");
  // check if the file is not open
  if (!input.is_open()) {
    // print error message and quit if a problem occurred
    std::cerr << "Error! No input file found!\n";
    exit(1);
  }
  // loop through each line in file
  std::string dummy;
  for (int i = 0; i < n; i++) {
    input >> dummy;
    // read in value, covert to int and write to array
    array[i] = std::stoi(dummy);
  }
  input.close();
}

void order_array(int array[], int n) {
  std::sort(array, array + n);  // ascending
  // std::sort(array, array + n, std::greater<int>());  // descending
}

void write_array_to_file(int array[], int n) {
  // create an output file stream
  std::ofstream output;
  // use it to open a file named 'output.txt'
  output.open("ordered.txt");
  // check if the file is not open
  if (!output.is_open()) {
    // print error message and quit if a problem occurred
    std::cerr << "Error creating file!\n";
    exit(1);
  }
  // loop through and print array
  for (int i = 0; i < n; i++) {
    output << array[i] << std::endl;
  }
  output.close();
}

Firstly 100 random numbers (in the range 0 to 100) are written to a text file. The random numbers are generated using the function covered in a previous lab.

The values in the file are then read into an array before the order_array() function sorts the data in the array using the std::sort function.

std::sort(array, array + n);

Here we pass in the beginning and end of the array. By default, the data will be sorted in ascending order. We can sort the data in descending order by doing the following instead.

std::sort(array, array + n, std::greater());

Output

The above example creates two files, 'random.txt' contains 100 random numbers in a random order and 'ordered.txt' contains the same random numbers in ascending order. Since the random number generator is seeded using the time, these numbers will be different every time. Sample outputs are shown below.

random.txt

24
17
30
17
36
68
26
94
6
14
47
2
50
5
38
92
65
99
66
100
30
69
71
47
47
63
50
26
43
57
37
17
66
14
13
88
7
74
99
42
64
35
11
6
71
90
18
32
0
4
84
86
44
79
89
16
69
27
18
63
10
3
1
65
59
60
64
26
3
56
5
83
27
3
32
89
5
1
92
18
70
74
5
94
16
41
42
21
77
52
56
62
77
43
16
74
73
12
6
61

ordered.txt

0
1
1
2
3
3
3
4
5
5
5
5
6
6
6
7
10
11
12
13
14
14
16
16
16
17
17
17
18
18
18
21
24
26
26
26
27
27
30
30
32
32
35
36
37
38
41
42
42
43
43
44
47
47
47
50
50
52
56
56
57
59
60
61
62
63
63
64
64
65
65
66
66
68
69
69
70
71
71
73
74
74
74
77
77
79
83
84
86
88
89
89
90
92
92
94
94
99
99
100