Text
bucket sort
Uses bucket sort on an array of integers. Prereq: void printArray(int [], int).
void bucketSort(int array[]) { 聽 聽聽int buckets[10][SIZE] = {0}; 聽 聽聽int largest = array[0]; 聽 聽聽int divider = 10; 聽 聽聽int count = 0; 聽 聽聽int place_value = 1; 聽 聽聽int digit = 0;聽 聽聽
聽 聽 for (int x = 0; x <= SIZE - 1; x++) 聽 聽 { 聽 聽 聽 聽聽if (array[x] > largest) 聽 聽 聽 聽 聽 聽 largest = array[x]; 聽 聽 }聽 聽聽
聽 聽 while (largest >= divider) 聽 聽 { 聽 聽 聽 聽聽for (int x = 0; x <= SIZE - 1; x++) 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 count = 0; 聽 聽 聽 聽 聽 聽聽if (place_value == 1) 聽 聽 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 聽 聽聽while (buckets[array[x] % divider][count]) 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 count++;
聽 聽 聽 聽 聽 聽 聽 聽 buckets[array[x] % divider][count] = array[x]; 聽 聽 聽 聽 聽 聽 } 聽 聽 聽 聽 聽 聽聽else if (place_value > 1) 聽 聽 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 聽 聽 digit = (array[x] / divider) % 10;
聽 聽 聽 聽 聽 聽 聽 聽 while (buckets[digit][count]) 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 count++;
聽 聽 聽 聽 聽 聽 聽 聽 buckets[digit][count] = array[x]; 聽 聽 聽 聽 聽 聽 } 聽 聽 聽 聽 }聽 聽 聽 聽聽
聽 聽 聽 聽 count = 0;聽 聽 聽 聽聽
聽 聽 聽 聽 for (int x = 0; x <= 9; x++) 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽聽for (int y = 0; y <= SIZE - 1; y++) 聽 聽 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 聽 聽聽if (buckets[x][y]) 聽 聽 聽 聽 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 array[count] = buckets[x][y]; 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 buckets[x][y] = 0; 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 count++; 聽 聽 聽 聽 聽 聽 聽 聽 } 聽 聽 聽 聽 聽 聽 } 聽 聽 聽 聽 }聽 聽 聽 聽聽
聽 聽 聽 聽 if (place_value > 1) 聽 聽 聽 聽 聽 聽 divider *= 10;聽 聽 聽 聽聽
聽 聽 聽 聽 place_value++; 聽 聽 }
聽 聽 printArray(array); }
1 note
路
View note
Text
prints a double array
Prints a double array.
void printDoubleArray(int array[][SIZE]) { 聽 聽聽for (int x = 0; x <= 9; x++) 聽 聽 { 聽 聽 聽 聽聽for (int y = 0; y <= SIZE - 1; y++) 聽 聽 聽 聽 聽 聽聽printf("%5d ", array[x][y]);聽 聽 聽 聽聽
聽 聽 聽 聽 printf("\n"); 聽 聽 }聽 聽聽
聽 聽 printf("\n"); }
1 note
路
View note
Text
merge sort
Uses merge sort on an array of integers.聽Prereqs: printArray(int [], int, int) & merge(int [], int, int, int, int).
void sortSubArray(int array[], int low, int high) { 聽 聽聽int middle1, middle2;聽 聽聽
聽 聽 if (low != high) 聽 聽 { 聽 聽 聽 聽 middle1 = (low + high) / 2; 聽 聽 聽 聽 middle2 = middle1 + 1;聽 聽 聽 聽聽
聽 聽 聽 聽 printf("LEFT: "); 聽 聽 聽 聽聽printArray(array, low, middle1); 聽 聽 聽 聽聽printf("RIGHT: "); 聽 聽 聽 聽聽printArray(array, middle2, high);聽 聽 聽 聽聽
聽 聽 聽 聽 sortSubArray(array, low, middle1); 聽 聽 聽 聽聽sortSubArray(array, middle2, high);聽 聽 聽 聽聽
聽 聽 聽 聽 merge(array, low, middle1, middle2, high); 聽 聽 }聽 聽聽
聽 聽 printf("\n"); }
1 note
路
View note
Text
merge into array
Merges two ordered sections of an array.聽Prereq: void printArray(int [], int, int).
void merge(int array[], int left, int middle1, int middle2, int right) { 聽 聽聽int temp[7] = {}; 聽 聽聽int count = 0; 聽 聽聽int start = left; 聽 聽聽int end = right;聽 聽聽
聽 聽 while (left <= middle1) 聽 聽 { 聽 聽 聽 聽聽if ((middle2 <= right) && (array[left] > array[middle2])) 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 temp[count] = array[middle2]; 聽 聽 聽 聽 聽 聽 middle2++; 聽 聽 聽 聽 } 聽 聽 聽 聽聽else 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 temp[count] = array[left]; 聽 聽 聽 聽 聽 聽 left++; 聽 聽 聽 聽 }聽 聽 聽 聽聽
聽 聽 聽 聽 count++; 聽 聽 }聽 聽聽
聽 聽 while (middle2 <= right) 聽 聽 { 聽 聽 聽 聽 temp[count] = array[middle2]; 聽 聽 聽 聽 count++; 聽 聽 聽 聽 middle2++; 聽 聽 }聽 聽聽
聽 聽 for (int x = start; x <= end; x++) 聽 聽 { 聽 聽 聽 聽 array[x] = temp[x - start]; 聽 聽 }聽 聽聽
聽 聽 printArray(array, 0, 6); }
0 notes
Text
prints array (selective)
Prints a certain section of an array.
void printArray(int array[], int start, int end) { 聽 聽聽for (int x = start; x <= end; x++) 聽 聽 { 聽 聽 聽 聽聽printf("%d ", array[x]); 聽 聽 }聽 聽聽
聽 聽 printf("\n"); }
0 notes
Text
insertion sort
Uses insertion sort on an array of integers. Prereq: void printArray(int[], int).
void聽insertionSort(int聽array[],聽int聽length) { 聽 聽聽int聽insert =聽0; 聽 聽聽int聽temp =聽0; 聽 聽聽int聽count =聽0;聽 聽聽
聽 聽聽for聽(int聽x =聽1; x < length; x++) 聽 聽 { 聽 聽 聽 聽聽int聽y =聽0;聽 聽 聽 聽聽
聽 聽 聽 聽聽while聽((array[y] < array[x]) && (y < x)) 聽 聽 聽 聽 聽 聽 y++;聽 聽 聽 聽聽
聽 聽 聽 聽 insert = y;
聽 聽 聽 聽聽if聽(insert != x) 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 temp = array[insert]; 聽 聽 聽 聽 聽 聽 array[insert] = array[x];聽 聽 聽 聽 聽 聽聽
聽 聽 聽 聽 聽 聽 count = x;
聽 聽 聽 聽 聽 聽聽while聽(count > insert +聽1) 聽 聽 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 聽 聽 array[count] = array[count -聽1]; 聽 聽 聽 聽 聽 聽 聽 聽 count--; 聽 聽 聽 聽 聽 聽 }聽 聽 聽 聽 聽 聽聽
聽 聽 聽 聽 聽 聽 array[count] = temp; 聽 聽 聽 聽 }聽 聽 聽 聽聽
聽 聽 聽 聽聽printArray(array, length); 聽 聽 } }
0 notes
Text
selection sort
Uses selection sort on an array of integers.聽Prereqs: void swap(int [], int, int) & void printArray(int [], int).
void selectionSort(int array[], int length) { 聽 聽聽int smallest = array[0]; 聽 聽聽int index_smallest = 0;聽 聽聽
聽 聽 for (int x = 0; x <= length - 1; x++) //goes through the entire array 聽 聽 { 聽 聽 聽 聽 smallest = array[x]; 聽 聽 聽 聽 index_smallest = x;聽 聽 聽 聽聽
聽 聽 聽 聽 for (int y = x; y <= length - 1; y++) 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽聽if (array[y] < smallest) 聽 聽 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽 聽 聽 smallest = array[y]; 聽 聽 聽 聽 聽 聽 聽 聽 index_smallest = y; 聽 聽 聽 聽 聽 聽 } 聽 聽 聽 聽 }聽 聽 聽 聽聽
聽 聽 聽 聽 if (index_smallest != x) 聽 聽 聽 聽 聽 聽聽swap(array, x, index_smallest);聽 聽 聽 聽聽
聽 聽 聽 聽 printArray(array, length); 聽 聽 }聽 聽聽 }
0 notes
Text
printArray
Prints an array.
void printArray(int array[], int size) { 聽 聽聽for (int x = 0; x < size; x++) 聽 聽 { 聽 聽 聽 聽聽printf("%d ", array[x]); 聽 聽 }聽 聽聽
聽 聽 printf("\n"); }
0 notes
Text
swap members of an array
Swaps two members of an array.
void swap(int array[], int first, int second) { 聽 聽聽int temp;
聽 聽 temp = array[first]; 聽 聽 array[first] = array[second]; 聽 聽 array[second] = temp; }
0 notes
Text
tree: printing
Prints a tree as it is.
void聽outputTree(TreeNode聽*t,聽int聽space) { 聽 聽聽TreeNode *current = t;聽 聽聽
聽 聽聽while聽(current !=聽NULL) 聽 聽 { 聽 聽 聽 聽聽if (current->right) 聽 聽 聽 聽 聽 聽聽outputTree(current->right, space + 5);聽 聽 聽 聽聽
聽 聽 聽 聽聽for聽(int聽x =聽0; x <= space -聽1; x++) 聽 聽 聽 聽 { 聽 聽 聽 聽 聽 聽聽printf(" "); 聽 聽 聽 聽 }聽 聽 聽 聽聽
聽 聽 聽 聽聽printf("%d\n", current->data);
聽 聽 聽 聽 current = current->left; 聽 聽 聽 聽 space += 5; 聽 聽 } }
0 notes
Text
tree: by level
Prints a tree according to the levels of the nodes. Prereqs: void enqueue(QueueNode **, TreeNode **)聽& TreeNode *dequeue(QueueNode **)
void levelOrder(TreeNode *tree) { 聽 聽 QueueNode *line = NULL; 聽 聽 TreeNode *current = tree;聽 聽聽
聽 聽 enqueue(&line, current);
聽 聽 while (line != NULL) 聽 聽 { 聽 聽 聽 聽 current = dequeue(&line); 聽 聽 聽 聽聽printf("%d ", current->data);
聽 聽 聽 聽 if (current->left) 聽 聽 聽 聽 聽 聽聽enqueue(&line, current->left); 聽 聽 聽 聽聽if (current->right) 聽 聽 聽 聽 聽 聽聽enqueue(&line, current->right); 聽 聽 } }
0 notes
Text
dequeue (tree version)
Removes a tree from a queue.
TreeNode *dequeue(QueueNode **back) { 聽 聽聽QueueNode *currentPtr = *back; 聽 聽聽QueueNode *previousPtr = NULL; 聽 聽聽TreeNode *value = NULL;聽 聽聽
聽 聽 while (currentPtr->nextPtr) 聽 聽 { 聽 聽 聽 聽 previousPtr = currentPtr; 聽 聽 聽 聽 currentPtr = currentPtr->nextPtr; 聽 聽 }聽 聽聽
聽 聽 value = currentPtr->tree; 聽 聽聽if (currentPtr != *back) 聽 聽 聽 聽聽free(currentPtr); 聽 聽聽else 聽 聽 聽 聽 *back = NULL;
聽 聽 if (previousPtr) 聽 聽 聽 聽 previousPtr->nextPtr = NULL;聽 聽聽
聽 聽 return value; }
0 notes
Text
enqueue (tree version)
Inserts a tree into a queue.
void enqueue(QueueNode **back, TreeNode *t) { 聽 聽聽QueueNode *node = malloc(sizeof(struct queueNode)); 聽 聽 node->tree = t;聽 聽聽
聽 聽 if (!*back) 聽 聽 聽 聽 node->nextPtr = NULL; 聽 聽聽else 聽 聽 聽 聽 node->nextPtr = *back;聽 聽聽
聽 聽 *back = node; }
0 notes
Text
circle
Parent Class: GraphicObject
@interface Circle : GraphicObject
- (void) setRadius: (int) r; - (void) setCenter: (XYPoint *) c; - (int) radius; - (XYPoint *) center; - (int) circumference; - (int) area;
@end
#import "Circle.h" #import "XYPoint.h"
@implementation Circle { 聽 聽聽int radius; 聽 聽聽XYPoint *center; }
- (void) setRadius: (int) r { 聽 聽聽radius = r; }
- (int) radius { 聽 聽聽return radius; }
- (void) setCenter: (XYPoint *) c { 聽 聽聽if (!center) 聽 聽 聽 聽聽center = [[XYPoint alloc] init];聽 聽聽
聽 聽 center.x = c.x; 聽 聽聽center.y = c.y; }
- (XYPoint *) center { 聽 聽聽return center; }
- (int) circumference { 聽 聽聽return 2 * 3.1416 * radius; }
- (int) area { 聽 聽聽return 3.1416 * radius * radius; }
@end
0 notes
Text
triangle
Parent Class: Graphic聽Object.
Prereq: XYPoint.
#import "GraphicObject.h" #import "XYPoint.h"
@interface Triangle : GraphicObject
@end聽
#import "Triangle.h" #import "XYPoint.h"
@implementation Triangle { 聽 聽聽int side; 聽 聽聽int altitude; 聽 聽聽XYPoint *center; }
- (void) setSide: (int) s { 聽 聽聽side = s; }
- (int) side { 聽 聽聽return side; }
- (void) setAltitude: (int) a { 聽 聽聽altitude = a; }
- (int) altitude { 聽 聽聽return altitude; }
- (void) setCenter: (XYPoint *) c; { 聽 聽聽if (!center) 聽 聽 聽 聽聽center = [[XYPoint alloc] init];
聽 聽 center.x = c.x; 聽 聽 center.y = c.y; }
- (XYPoint *) center { 聽 聽聽return center; }
- (int) perimeter { 聽 聽聽return side * 3; }
- (int) area { 聽 聽聽return side * altitude / 2; }
@end
0 notes
Text
graphic object
Subclasses: Circle, Rectangle, Triangle.
@interface GraphicObject : NSObject
- (void) setFillColor: (int) i; - (int) fillColor; - (void) setFilled: (BOOL) b; - (BOOL) filled; - (void) setLineColor: (int) i; - (int) lineColor;
@end聽
#import "GraphicObject.h"
@implementation GraphicObject {
聽 聽聽int fillColor; 聽 聽聽BOOL filled; 聽 聽聽int lineColor; }
- (void) setFillColor: (int) i { 聽 聽聽fillColor = i; }
- (int) fillColor { 聽 聽聽return fillColor; }
- (void) setFilled: (BOOL) b { 聽 聽聽filled = b; }
- (BOOL) filled { 聽 聽 return filled; }
- (void) setLineColor: (int) i { 聽 聽聽lineColor = i; }
- (int) lineColor { 聽 聽聽return lineColor; }
@end
0 notes
Text
X-Y Point
@interface XYPoint : NSObject
@property int x, y;
- (void) print
@end聽
#import "XYPoint.h"
@implementation XYPoint
@synthesize x, y;
- (void) print { 聽 聽聽NSLog(@"(%i, %i)", x, y); }
@end
0 notes