Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <string.h>
- #include <time.h>
- typedef int bool;
- #define true 1
- #define false 0
- //prototypes
- void DumpMemory(void * mem, int sizeinbytes);
- void SelectSort(int * array, int size);
- int * CreateArray(int size);
- void PrintArray(int * array, int size);
- int BinarySeach(int value, int * array, int size);
- void SelectSort(int * array, int size){
- int i, j, temp, index;
- if (size <= 0)
- return;
- //Loop all values
- for (i = 0; i < size;i++){
- index = i;
- //Look for a lower value and get its index
- for (j = i; j < size; j++){
- if (array[j]<array[index]){
- index = j;
- }
- }
- //if there is an index with a lower value then ours, swap
- if (index != i){
- temp = array[index];
- array[index] = array[i];
- array[i] = temp;
- }
- }
- }
- //Dynamically create an array
- //This memory has to be free'd
- int * CreateArray(int size){
- //Gives you the pointer to a memory space in bytes
- //sizeof(type) returns the size in bytes of a type
- //ie; sizeof(int) = 4 * size you want
- int * array = (int*)malloc(sizeof(int)*size);
- int n;
- //Ensure we got the memory, malloc returns NULL if it fails
- assert(array!=NULL);
- //At this point the memory of malloc contains whatever data was in the memory space
- //we can fix this by using calloc to allocate memory or by doing this:
- //memset will set all bytes in the array according to size to val, (null in our case)
- memset(array, NULL, sizeof(int)*size);
- //Fill array with numbers between 0 and 999
- for (n = 0; n < size; n++){
- array[n] = rand() % 1000;
- }
- return array;
- }
- //Dump out the memory
- void DumpMemory(void * mem, int sizeinbytes){
- //assert(sizeinbytes>4);
- int sizeoftype = sizeof(int);
- unsigned char * raw = (unsigned char*)mem;
- puts("-------------------------------------------------");
- int i;
- bool first = true;
- for (i = 0; i < sizeinbytes; i++){
- if (first || i % sizeoftype == 0){
- if (first){
- printf("%08X: ", &raw[i]);
- first = false;
- }
- else
- printf("%d\n%08X: ", *(int*)&raw[i-4], &raw[i]);
- }
- printf("%02X ", raw[i]);
- }
- printf("%d\n-------------------------------------------------\n", *(int*)&raw[i - 4]);
- }
- //does what you think it does
- void PrintArray(int * array, int size){
- int n;
- for (n = 0; n < size; n++){
- printf("%3d ",array[n]);
- }
- printf("\n");
- }
- int * BinaryPointerSeach(int value, int * lower, int * upper){
- int * middle = ((upper - lower) / 2) + lower;
- if (middle >= upper || middle <= lower){
- if ((middle == upper || middle == lower) && *middle == value)
- return middle;
- else
- return NULL;
- }
- else if(*middle == value){
- return middle;
- }
- else if (*middle < value){
- return BinaryPointerSeach(value, middle, upper);
- }
- else{
- return BinaryPointerSeach(value, lower, middle);
- }
- }
- int BinarySeach(int value, int * array, int size){
- //Get middle value
- int middle = (size / 2)-1;
- int n;
- //If middle value is zero we can't divide anymore, seach what we got
- if (middle == 0){
- for (n = 0; n < size;n++){
- //found it?
- if (array[n] == value)
- return n;
- }
- //doesnt exist
- return -1;
- }
- //We're at the value, return!
- else if (array[middle] == value)
- return middle;
- else if (value>array[middle]){
- //Recursion the upper half of the array
- n = BinarySeach(value, &array[middle], size - middle);
- //not found, pass the not found index down the callstack
- if (n == -1)
- return -1;
- //Found, correct the value to the array as a whole
- else
- middle = size-((size - middle) - n);
- }
- else{
- //recurion on the lower half
- n = BinarySeach(value, array, middle);
- //doesnt exist, pass it down
- if (n == -1)
- return -1;
- //we're at the lowerhalf, we can just return what we got
- else
- middle = n;
- }
- return middle;
- }
- int main()
- {
- //How many numbers should the array contain
- int size = 10;
- int * array;
- int index;
- int numbertofind;
- //seed the psuedo random number generator, rng demands blood
- srand(time(NULL));
- //Create the array
- array = CreateArray(size);
- //Grab a random number we know exist, this is before its sorted
- numbertofind = array[size-1];
- //Print what the values are
- printf("Before sort: ");
- PrintArray(array, size);
- //print what the memory looks like
- DumpMemory(array, sizeof(int)*size);
- //SORT IT!
- SelectSort(array, size);
- //Then print what it looks like now
- printf("After sort: ");
- PrintArray(array, size);
- DumpMemory(array, sizeof(int)*size);
- //Binary seach a value
- index = BinarySeach(numbertofind, array, size);
- //It wasnt found
- if (index == -1){
- printf("Could not find %d in the array!\n", numbertofind);
- }
- //If was found, print the value we where looking for, the value from the array using the index and the index
- else{
- printf("%d = %d was found on index %d in the array\n", numbertofind,array[index], index);
- }
- //Find by pointer
- int * find = BinaryPointerSeach(numbertofind, array, &array[size]);
- if (find == NULL)
- printf("didnt find %d\n", numbertofind);
- else {
- printf("%d was found on address %08X deref: %d index: %d\n", numbertofind, find, *find, find-array);
- DumpMemory(find, sizeof(int));
- }
- //Always clean up your memory, give it back to the OS
- free(array);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement