Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 17, 2016
0 parents
commit 0fea696
Showing
101 changed files
with
3,441 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2013 Noel Kalicharan | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Program P10.1 | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#define MaxHeapSize 100 | ||
int main() { | ||
void siftUp(int[], int); | ||
int num[MaxHeapSize + 1]; | ||
int n = 0, number; | ||
FILE * in = fopen("heap.in", "r"); | ||
|
||
while (fscanf(in, "%d", &number) == 1) { | ||
if (n < MaxHeapSize) { //check if array has room | ||
num[++n] = number; | ||
siftUp(num, n); | ||
} | ||
else { | ||
printf("\nArray too small\n"); | ||
exit(1); | ||
} | ||
} | ||
for (int h = 1; h <= n; h++) printf("%d ", num[h]); | ||
printf("\n"); | ||
fclose(in); | ||
} //end main | ||
|
||
void siftUp(int heap[], int n) { | ||
//sifts up the value in heap[n] so that heap[1..n] contains a heap | ||
int siftItem = heap[n]; | ||
int child = n; | ||
int parent = child / 2; | ||
while (parent > 0) { | ||
if (siftItem <= heap[parent]) break; | ||
heap[child] = heap[parent]; //move down parent | ||
child = parent; | ||
parent = child / 2; | ||
} | ||
heap[child] = siftItem; | ||
} //end siftUp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdio.h> | ||
|
||
int main() { | ||
void heapSort(int[], int); | ||
int num[] = {0, 37, 25, 43, 65, 48, 84, 73, 18, 79, 56, 69, 32}; | ||
int n = 12; | ||
heapSort(num, n); | ||
for (int h = 1; h <= n; h++) printf("%d ", num[h]); | ||
printf("\n"); | ||
} //end main | ||
|
||
void siftDown(int key, int num[], int root, int last) { | ||
int bigger = 2 * root; | ||
while (bigger <= last) { //while there is at least one child | ||
if (bigger < last) //there is a right child as well; find the bigger | ||
if (num[bigger+1] > num[bigger]) bigger++; | ||
//'bigger' holds the index of the bigger child | ||
if (key >= num[bigger]) break; | ||
//key is smaller; promote num[bigger] | ||
num[root] = num[bigger]; | ||
root = bigger; | ||
bigger = 2 * root; | ||
} | ||
num[root] = key; | ||
} //end siftDown | ||
|
||
void heapSort(int num[], int n) { | ||
//sort num[1] to num[n] | ||
void siftDown(int, int[], int, int); | ||
//convert the array to a heap | ||
for (int h = n / 2; h >= 1; h--) siftDown(num[h], num, h, n); | ||
|
||
for (int k = n; k > 1; k--) { | ||
int item = num[k]; //extract current last item | ||
num[k] = num[1]; //move top of heap to current last node | ||
siftDown(item, num, 1, k-1); //restore heap properties from 1 to k-1 | ||
} | ||
} //end heapSort | ||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdio.h> | ||
|
||
int main() { | ||
void quicksort2(int[], int, int); | ||
int num[] = {0, 53, 12, 98, 63, 18, 32, 80, 46, 72, 21}; | ||
int n = 10; | ||
quicksort2(num, 1, n); | ||
for (int h = 1; h <= n; h++) printf("%d ", num[h]); | ||
printf("\n"); | ||
} //end main | ||
|
||
void quicksort2(int A[], int lo, int hi) { | ||
//sorts A[lo] to A[hi] in ascending order | ||
int partition2(int[], int, int); | ||
if (lo < hi) { | ||
int dp = partition2(A, lo, hi); | ||
quicksort2(A, lo, dp); | ||
quicksort2(A, dp+1, hi); | ||
} | ||
} //end quicksort2 | ||
|
||
int partition2(int A[], int lo, int hi) { | ||
//return dp such that A[lo..dp] <= A[dp+1..hi] | ||
void swap(int[], int, int); | ||
int pivot = A[lo]; | ||
--lo; ++hi; | ||
while (lo < hi) { | ||
do --hi; while (A[hi] > pivot); | ||
do ++lo; while (A[lo] < pivot); | ||
if (lo < hi) swap(A, lo, hi); | ||
} | ||
return hi; | ||
} //end partition2 | ||
|
||
void swap(int list[], int i, int j) { | ||
//swap list[i] and list[j] | ||
int hold = list[i]; | ||
list[i] = list[j]; | ||
list[j] = hold; | ||
} //end swap |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main() { | ||
void quicksort2(int[], int, int); | ||
int num[] = {0, 53, 12, 98, 63, 18, 32, 80, 46, 72, 21}; | ||
int n = 10; | ||
quicksort2(num, 1, n); | ||
for (int h = 1; h <= n; h++) printf("%d ", num[h]); | ||
printf("\n"); | ||
} //end main | ||
|
||
void quicksort2(int A[], int lo, int hi) { | ||
//sorts A[lo] to A[hi] in ascending order | ||
int partition2a(int[], int, int); | ||
if (lo < hi) { | ||
int dp = partition2a(A, lo, hi); | ||
quicksort2(A, lo, dp); | ||
quicksort2(A, dp+1, hi); | ||
} | ||
} //end quicksort2 | ||
|
||
int partition2a(int A[], int lo, int hi) { | ||
//return dp such that A[lo..dp] <= A[dp+1..hi] | ||
void swap(int[], int, int); | ||
int random(int, int); | ||
//choose a random pivot | ||
swap(A, lo, random(lo, hi)); | ||
int pivot = A[lo]; | ||
--lo; ++hi; | ||
while (lo < hi) { | ||
do --hi; while (A[hi] > pivot); | ||
do ++lo; while (A[lo] < pivot); | ||
if (lo < hi) swap(A, lo, hi); | ||
} | ||
return hi; | ||
} //end partition2a | ||
|
||
void swap(int list[], int i, int j) { | ||
//swap list[i] and list[j] | ||
int hold = list[i]; | ||
list[i] = list[j]; | ||
list[j] = hold; | ||
} //end swap | ||
|
||
int random(int m, int n) { | ||
//returns a random integer from m to n, inclusive | ||
int offset = rand()/(RAND_MAX + 1.0) * (n - m + 1); | ||
return m + offset; | ||
} //end random |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct { | ||
int left, right; | ||
} StackData; | ||
|
||
#include <stack.h> | ||
|
||
int main() { | ||
void quicksort3(int[], int, int); | ||
int num[] = {0, 53, 12, 98, 63, 18, 32, 80, 46, 72, 21}; | ||
int n = 10; | ||
quicksort3(num, 1, n); | ||
for (int h = 1; h <= n; h++) printf("%d ", num[h]); | ||
printf("\n"); | ||
} //end main | ||
|
||
void quicksort3(int A[], int lo, int hi) { | ||
int partition2a(int[], int, int); | ||
StackData newStackData(int, int); | ||
Stack S = initStack(); | ||
push(S, newStackData(lo, hi)); | ||
int stackItems = 1, maxStackItems = 1; | ||
while (!empty(S)) { | ||
--stackItems; | ||
StackData d = pop(S); | ||
if (d.left < d.right) { //if the sublist is > 1 element | ||
int dp = partition2a(A, d.left, d.right); | ||
if (dp - d.left + 1 < d.right - dp) { //compare lengths of sublists | ||
push(S, newStackData(dp+1, d.right)); | ||
push(S, newStackData(d.left, dp)); | ||
} | ||
else { | ||
push(S, newStackData(d.left, dp)); | ||
push(S, newStackData(dp+1, d.right)); | ||
} | ||
stackItems += 2; //two items added to stack | ||
} //end if | ||
if (stackItems > maxStackItems) maxStackItems = stackItems; | ||
} //end while | ||
printf("Max stack items: %d\n\n", maxStackItems); | ||
} //end quicksort3 | ||
|
||
int partition2a(int A[], int lo, int hi) { | ||
//return dp such that A[lo..dp] <= A[dp+1..hi] | ||
void swap(int[], int, int); | ||
int random(int, int); | ||
//choose a random pivot | ||
swap(A, lo, random(lo, hi)); | ||
int pivot = A[lo]; | ||
--lo; ++hi; | ||
while (lo < hi) { | ||
do --hi; while (A[hi] > pivot); | ||
do ++lo; while (A[lo] < pivot); | ||
if (lo < hi) swap(A, lo, hi); | ||
} | ||
return hi; | ||
} //end partition2a | ||
|
||
void swap(int list[], int i, int j) { | ||
//swap list[i] and list[j] | ||
int hold = list[i]; | ||
list[i] = list[j]; | ||
list[j] = hold; | ||
} //end swap | ||
|
||
int random(int m, int n) { | ||
//returns a random integer from m to n, inclusive | ||
int offset = rand()/(RAND_MAX + 1.0) * (n - m + 1); | ||
return m + offset; | ||
} //end random | ||
|
||
StackData newStackData(int a, int b) { | ||
StackData temp; | ||
temp.left = a; | ||
temp.right = b; | ||
return temp; | ||
} //end newStackData | ||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <stdio.h> | ||
|
||
int main() { | ||
void quicksort(int[], int, int); | ||
int num[] = {0, 53, 12, 98, 63, 18, 32, 80, 46, 72, 21}; | ||
int n = 10; | ||
quicksort(num, 1, n); | ||
for (int h = 1; h <= n; h++) printf("%d ", num[h]); | ||
printf("\n"); | ||
} //end main | ||
|
||
void quicksort(int A[], int lo, int hi) { | ||
//sorts A[lo] to A[hi] in ascending order | ||
int partition1(int[], int, int); | ||
if (lo < hi) { | ||
int dp = partition1(A, lo, hi); | ||
quicksort(A, lo, dp-1); | ||
quicksort(A, dp+1, hi); | ||
} | ||
} //end quicksort | ||
|
||
int partition1(int A[], int lo, int hi) { | ||
//partition A[lo] to A[hi] using A[lo] as the pivot | ||
void swap(int[], int, int); | ||
int pivot = A[lo]; | ||
int lastSmall = lo; | ||
for (int h = lo + 1; h <= hi; h++) | ||
if (A[h] < pivot) { | ||
++lastSmall; | ||
swap(A, lastSmall, h); | ||
} | ||
//end for | ||
swap(A, lo, lastSmall); | ||
return lastSmall; //return the division point | ||
} //end partition1 | ||
|
||
void swap(int list[], int i, int j) { | ||
//swap list[i] and list[j] | ||
int hold = list[i]; | ||
list[i] = list[j]; | ||
list[j] = hold; | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Shell sort | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#define MaxSize 100 | ||
int main() { | ||
void hsort(int[], int, int); | ||
int num[MaxSize + 1]; | ||
int n = 0, number; | ||
FILE * in = fopen("shell.in", "r"); | ||
|
||
int incr[] = {3, 8, 3, 1}; //first 3 is the number of increments | ||
|
||
while (fscanf(in, "%d", &number) == 1) { | ||
if (n < MaxSize) num[++n] = number; | ||
else { | ||
printf("\nArray too small\n"); | ||
exit(1); | ||
} | ||
} | ||
//perform Shell sort with increments 8, 3 and 1 | ||
// hsort(num, n, 8); | ||
// hsort(num, n, 3); | ||
// hsort(num, n, 1); | ||
|
||
for (int i = 1; i <= incr[0]; i++) hsort(num, n, incr[i]); | ||
|
||
for (int h = 1; h <= n; h++) printf("%d ", num[h]); | ||
printf("\n"); | ||
fclose(in); | ||
} //end main | ||
|
||
void hsort(int A[], int n, int h) { | ||
for (int k = h + 1; k <= n; k++) { | ||
int j = k - h; //j will index elements k - h, k - 2h, k - 3h, etc | ||
int key = A[k]; | ||
while (j > 0 && key < A[j]) { | ||
A[j + h] = A[j]; | ||
j = j - h; | ||
} | ||
A[j + h] = key; | ||
} | ||
} //end hsort |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Program P11.1 | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#define MaxNumbers 20 | ||
#define N 23 | ||
#define Empty 0 | ||
|
||
int main() { | ||
FILE * in = fopen("numbers.in", "r"); | ||
int h, key, num[N + 1]; | ||
for (h = 1; h <= N; h++) num[h] = Empty; | ||
int distinct = 0; | ||
while (fscanf(in, "%d", &key) == 1) { | ||
int loc = key % N + 1; | ||
while (num[loc] != Empty && num[loc] != key) loc = loc % N + 1; | ||
if (num[loc] == Empty) { //key is not in the table | ||
if (distinct == MaxNumbers) { | ||
printf("\nTable full: %d not added\n", key); | ||
exit(1); | ||
} | ||
num[loc] = key; | ||
distinct++; | ||
} | ||
} | ||
printf("\nThere are %d distinct numbers\n", distinct); | ||
fclose(in); | ||
} //end main | ||
|
Oops, something went wrong.