-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqsort.c
More file actions
128 lines (93 loc) · 2.4 KB
/
qsort.c
File metadata and controls
128 lines (93 loc) · 2.4 KB
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
123
124
125
126
127
128
/*
* File: qsort.c
*
* Copyright: 2020, Darren C. Atkinson
*
* Description: Reads words from a text file whose name is given as the
* first and only command-line argument. The words are stored
* in a list that is then sorted using quicksort, and the words
* are then displayed in sorted order.
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "list.h"
# define MAX_WORD_LENGTH 30 /* maximum length of a single word */
/*
* Function: partition
*
* Description: Choose the first element in the sublist as the pivot and
* partition the sublist around the pivot. Hoare's partition
* scheme is used: https://en.wikipedia.org/wiki/Quicksort.
*/
static int partition(LIST *lp, int lo, int hi)
{
int i, j;
char *temp, *x;
x = getItem(lp, lo);
i = lo - 1;
j = hi + 1;
while (i < j) {
do
j = j - 1;
while (strcmp(getItem(lp, j), x) > 0);
do
i = i + 1;
while (strcmp(getItem(lp, i), x) < 0);
if (i < j) {
temp = getItem(lp, i);
setItem(lp, i, getItem(lp, j));
setItem(lp, j, temp);
}
}
return j;
}
/*
* Function: quickSort
*
* Description: Recursively sort the given sublist using the quicksort
* sorting algorithm: partition the sublist around a pivot,
* recursively sort the lower half of the list, and finally
* recursively sort the upper half of the list.
*/
static void quickSort(LIST *lp, int lo, int hi)
{
int i;
if (hi > lo) {
i = partition(lp, lo, hi);
quickSort(lp, lo, i);
quickSort(lp, i + 1, hi);
}
}
/*
* Function: main
*
* Description: Driver function for the qsort application.
*/
int main(int argc, char *argv[])
{
FILE *fp;
LIST *words;
char word[MAX_WORD_LENGTH+1];
/* Check the number of arguments and try to open the file. */
if (argc != 2) {
fprintf(stderr, "missing filename\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "cannot open file\n");
exit(EXIT_FAILURE);
}
/* Read each word into the buffer and add it to the list. */
words = createList();
while (fscanf(fp, "%s", word) == 1)
addLast(words, strdup(word));
fclose(fp);
/* Sort the words in the list and print them out in sorted order. */
quickSort(words, 0, numItems(words) - 1);
while (numItems(words) > 0)
printf("%s\n", (char *) removeFirst(words));
destroyList(words);
exit(EXIT_SUCCESS);
}