Advertisement
Terrah

Select Sort, Binary Seach, Dynamic memory

May 3rd, 2015
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. typedef int bool;
  8. #define true 1
  9. #define false 0
  10.  
  11. //prototypes
  12. void DumpMemory(void * mem, int sizeinbytes);
  13. void SelectSort(int * array, int size);
  14. int * CreateArray(int size);
  15. void PrintArray(int * array, int size);
  16. int BinarySeach(int value, int * array, int size);
  17.  
  18. void SelectSort(int * array, int size){
  19.  
  20.     int i, j, temp, index;
  21.  
  22.     if (size <= 0)
  23.         return;
  24.  
  25.     //Loop all values
  26.     for (i = 0; i < size;i++){
  27.  
  28.         index = i;
  29.  
  30.         //Look for a lower value and get its index
  31.         for (j = i; j < size; j++){
  32.             if (array[j]<array[index]){
  33.                 index = j;
  34.             }
  35.         }
  36.  
  37.         //if there is an index with a lower value then ours, swap
  38.         if (index != i){
  39.             temp = array[index];
  40.             array[index] = array[i];
  41.             array[i] = temp;
  42.         }
  43.     }
  44. }
  45.  
  46. //Dynamically create an array
  47. //This memory has to be free'd
  48. int * CreateArray(int size){
  49.  
  50.     //Gives you the pointer to a memory space in bytes
  51.     //sizeof(type) returns the size in bytes of a type
  52.     //ie; sizeof(int) = 4 * size you want
  53.     int * array = (int*)malloc(sizeof(int)*size);
  54.     int n;
  55.  
  56.     //Ensure we got the memory, malloc returns NULL if it fails
  57.     assert(array!=NULL);
  58.  
  59.     //At this point the memory of malloc contains whatever data was in the memory space
  60.     //we can fix this by using calloc to allocate memory or by doing this:
  61.  
  62.     //memset will set all bytes in the array according to size to val, (null in our case)
  63.     memset(array, NULL, sizeof(int)*size);
  64.  
  65.     //Fill array with numbers between 0 and 999
  66.     for (n = 0; n < size; n++){
  67.         array[n] = rand() % 1000;
  68.     }
  69.  
  70.     return array;
  71. }
  72.  
  73. //Dump out the memory
  74. void DumpMemory(void * mem, int sizeinbytes){
  75.    
  76.     //assert(sizeinbytes>4);
  77.     int sizeoftype = sizeof(int);
  78.     unsigned char * raw = (unsigned char*)mem;
  79.  
  80.  
  81.     puts("-------------------------------------------------");
  82.     int i;
  83.     bool first = true;
  84.     for (i = 0; i < sizeinbytes; i++){
  85.  
  86.         if (first || i % sizeoftype == 0){
  87.             if (first){
  88.                 printf("%08X: ", &raw[i]);
  89.                 first = false;
  90.             }
  91.             else
  92.                 printf("%d\n%08X: ", *(int*)&raw[i-4], &raw[i]);
  93.         }
  94.  
  95.         printf("%02X ", raw[i]);
  96.     }
  97.     printf("%d\n-------------------------------------------------\n", *(int*)&raw[i - 4]);
  98. }
  99.  
  100. //does what you think it does
  101. void PrintArray(int * array, int size){
  102.     int n;
  103.     for (n = 0; n < size; n++){
  104.         printf("%3d ",array[n]);
  105.     }
  106.     printf("\n");
  107. }
  108.  
  109. int * BinaryPointerSeach(int value, int * lower, int * upper){
  110.  
  111.     int * middle = ((upper - lower) / 2) + lower;
  112.  
  113.     if (middle >= upper || middle <= lower){
  114.  
  115.         if ((middle == upper || middle == lower) && *middle == value)
  116.             return middle;
  117.         else
  118.             return NULL;
  119.     }
  120.     else if(*middle == value){
  121.         return middle;
  122.     }
  123.     else if (*middle < value){
  124.         return BinaryPointerSeach(value, middle, upper);
  125.     }
  126.     else{
  127.         return BinaryPointerSeach(value, lower, middle);
  128.     }
  129. }
  130.  
  131. int BinarySeach(int value, int * array, int size){
  132.  
  133.     //Get middle value
  134.     int middle = (size / 2)-1;
  135.     int n;
  136.  
  137.     //If middle value is zero we can't divide anymore, seach what we got
  138.     if (middle == 0){
  139.         for (n = 0; n < size;n++){
  140.             //found it?
  141.             if (array[n] == value)
  142.                 return n;
  143.         }
  144.         //doesnt exist
  145.         return -1;
  146.     }
  147.     //We're at the value, return!
  148.     else if (array[middle] == value)
  149.         return middle;
  150.     else if (value>array[middle]){
  151.         //Recursion the upper half of the array
  152.         n = BinarySeach(value, &array[middle], size - middle); 
  153.         //not found, pass the not found index down the callstack
  154.         if (n == -1)
  155.             return -1;
  156.         //Found, correct the value to the array as a whole
  157.         else
  158.             middle = size-((size - middle) - n);
  159.     }
  160.     else{
  161.         //recurion on the lower half
  162.         n = BinarySeach(value, array, middle);
  163.         //doesnt exist, pass it down
  164.         if (n == -1)
  165.             return -1;
  166.         //we're at the lowerhalf, we can just return what we got
  167.         else
  168.             middle = n;
  169.  
  170.     }  
  171.  
  172.     return middle;
  173. }
  174.  
  175. int main()
  176. {
  177.    
  178.     //How many numbers should the array contain
  179.     int size = 10;
  180.     int * array;
  181.     int index;
  182.     int numbertofind;
  183.  
  184.     //seed the psuedo random number generator, rng demands blood
  185.     srand(time(NULL));
  186.  
  187.     //Create the array
  188.     array = CreateArray(size);
  189.    
  190.     //Grab a random number we know exist, this is before its sorted
  191.     numbertofind = array[size-1];
  192.  
  193.     //Print what the values are
  194.     printf("Before sort: ");
  195.     PrintArray(array, size);
  196.  
  197.     //print what the memory looks like
  198.     DumpMemory(array, sizeof(int)*size);
  199.  
  200.     //SORT IT!
  201.     SelectSort(array, size);
  202.  
  203.     //Then print what it looks like now
  204.     printf("After sort:  ");
  205.     PrintArray(array, size);
  206.     DumpMemory(array, sizeof(int)*size);
  207.  
  208.     //Binary seach a value
  209.     index = BinarySeach(numbertofind, array, size);
  210.  
  211.     //It wasnt found
  212.     if (index == -1){
  213.         printf("Could not find %d in the array!\n", numbertofind);
  214.     }
  215.     //If was found, print the value we where looking for, the value from the array using the index and the index
  216.     else{
  217.         printf("%d = %d was found on index %d in the array\n", numbertofind,array[index], index);
  218.     }
  219.  
  220.     //Find by pointer
  221.     int * find = BinaryPointerSeach(numbertofind, array, &array[size]);
  222.  
  223.     if (find == NULL)
  224.         printf("didnt find %d\n", numbertofind);
  225.     else {
  226.         printf("%d was found on address %08X deref: %d index: %d\n", numbertofind, find, *find, find-array);
  227.         DumpMemory(find, sizeof(int));
  228.     }
  229.     //Always clean up your memory, give it back to the OS
  230.     free(array);
  231.  
  232.     _getch();
  233.  
  234.     return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement