MBJ

Stack_Error_Sorting_C_File

MBJ
Jun 19th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. /* contains the bubbleSort algorithm */
  2. /* Dependency: bubbleSort.h */
  3. /* Target: bubbleSort.o */
  4.  
  5. /*  What is bubble sorting?
  6.         "a simple sorting algorithm that repeatedly steps through the list, compares adjacent
  7.             elements and swaps them if they are in the wrong order. The pass through the list
  8.             is repeated until the list is sorted. The algorithm, which is a comparison sort, is
  9.             named for the way smaller or larger elements "bubble" to the top of the list."
  10.                                                             - https://en.wikipedia.org/wiki/Bubble_sort
  11.  
  12.  
  13. */
  14.  
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18.  
  19. void sort(long *ar_ptr/* ptr to beginning of array */,uint8_t size){
  20.     bool finished_sort_flag = false;   
  21.     uint8_t element, i;
  22.     long temp;
  23.     long *temp_ptr;
  24.     while(!finished_sort_flag){
  25.         temp_ptr = ar_ptr;
  26.         finished_sort_flag = true; /* only gets reverted if a pass thru changes elements */
  27.         for(i = 0; i < size-1 /* using nth and n+1 elements per loop */; i++){
  28.             if(*(temp_ptr) > *(temp_ptr + 1)){ /* the next element is smaller than the previous */
  29.                 //printf("Old order: %ld\t%ld\n",*(ar_ptr),*(ar_ptr + 1));
  30.                 temp = *(temp_ptr);
  31.                 *(temp_ptr) = *(temp_ptr + 1);
  32.                 *(temp_ptr + 1) = temp;
  33.                 finished_sort_flag = false;
  34.                 //printf("New order: %ld\t%ld\n",*(ar_ptr),*(ar_ptr + 1));
  35.             }
  36.             //printf("old ar_ptr: %lp\n",temp_ptr);
  37.             temp_ptr = temp_ptr + 1; /* pointer arithmetic */
  38.             //printf("New ar_ptr: %lp\n",temp_ptr);
  39.         }
  40.  
  41.     }
  42. /*
  43.     for(i = 0; i < size; i++){
  44.         //printf("%d\n",i);
  45.         printf("Element %d: %ld\n",i,*(ar_ptr + i));
  46.     }
  47. */
  48.     //return ar_ptr;
  49. }
Add Comment
Please, Sign In to add comment