forked from yunghsianglu/CProgramExercise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortable.c
More file actions
140 lines (130 loc) · 3.16 KB
/
Copy pathsortable.c
File metadata and controls
140 lines (130 loc) · 3.16 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
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int findIndex(int * arr, int first, int last, int maxmin)
// find the index of the largest or smallest element of the array
// the range is expressed by the indexes [first, last]
// maxmin = 1: find largest, maxmin = 0: find smallest
{
int ind;
int answer = first;
for (ind = first + 1; ind <= last; ind ++)
{
if (((maxmin == 1) && (arr[answer] < arr[ind])) ||
((maxmin == 0) && (arr[answer] > arr[ind])))
{
answer = ind;
}
}
return answer;
}
int findMaxIndex(int * arr, int first, int last)
{
return findIndex(arr, first, last, 1);
}
int findMinIndex(int * arr, int first, int last)
{
return findIndex(arr, first, last, 0);
}
int isStackSortable(int * arr, int first, int last)
// check whether the range of the array is stack sortable
// return 1 if the range of the array is stack sortable
// return 0 if the range of the array is not stack sortable
{
if (first >= last) // no or one element is stack sortable
{
return 1;
}
int maxIndex = findMaxIndex(arr, first, last);
// consider the four cases
// both A and B are empty
// The array has only one element, it is stack sortable
// already checked earlier
// A is empty, B is not empty
// check whether B is stack sortable
if (first == maxIndex)
{
return isStackSortable(arr, first + 1, last);
}
// A is not empty, B is empty
// check whether A is stack sortable
if (maxIndex == last)
{
return isStackSortable(arr, first, last - 1);
}
// neither is empty
int maxAIndex = findMaxIndex(arr, first, maxIndex - 1);
int minBIndex = findMinIndex(arr, maxIndex + 1, last);
if (arr[maxAIndex] > arr[minBIndex])
{
return 0; // not stack sortable
}
int sortA = isStackSortable(arr, first, maxIndex - 1);
int sortB = isStackSortable(arr, maxIndex + 1, last);
return (sortA && sortB); // return 1 only if both are 1
}
void printArray(int * arr, int length)
{
if (isStackSortable(arr, 0, length - 1) == 0)
{
return;
}
int ind;
for (ind = 0; ind < length - 1; ind ++)
{
printf("%d", arr[ind]);
}
printf("%d\n", arr[length - 1]);
}
void swap(int * a, int * b)
{
int s = * a;
* a = * b;
* b = s;
}
void permuteHelp(int * arr, int ind, int num)
{
if (ind == num)
{
printArray(arr, ind);
return;
}
int loc; // destination of arr[ind]
for (loc = ind; loc < num; loc ++)
{
swap(& arr[ind], & arr[loc]);
permuteHelp(arr, ind + 1, num);
swap(& arr[ind], & arr[loc]); // swap back
}
}
void permute(int * arr, int num)
{
permuteHelp(arr, 0, num);
}
int main(int argc, char * argv[])
{
if (argc != 2)
{
return EXIT_FAILURE;
}
int num = (int) strtol(argv[1], NULL, 10);
if (num <= 0)
{
return EXIT_FAILURE;
}
int * arr;
arr = malloc(sizeof(int) * num);
int ind;
for (ind = 0; ind < num; ind ++)
{
arr[ind] = ind + 1;
}
permute(arr, num);
free (arr);
return EXIT_SUCCESS;
}
/*
* This program was originally written by Mr. Aaron Michaux.
* I modify the program so that the style is consistent with
* the other programs in this book.
*/