Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* contains the bubbleSort algorithm */
- /* Dependency: bubbleSort.h */
- /* Target: bubbleSort.o */
- /* What is bubble sorting?
- "a simple sorting algorithm that repeatedly steps through the list, compares adjacent
- elements and swaps them if they are in the wrong order. The pass through the list
- is repeated until the list is sorted. The algorithm, which is a comparison sort, is
- named for the way smaller or larger elements "bubble" to the top of the list."
- - https://en.wikipedia.org/wiki/Bubble_sort
- */
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdio.h>
- void sort(long *ar_ptr/* ptr to beginning of array */,uint8_t size){
- bool finished_sort_flag = false;
- uint8_t element, i;
- long temp;
- long *temp_ptr;
- while(!finished_sort_flag){
- temp_ptr = ar_ptr;
- finished_sort_flag = true; /* only gets reverted if a pass thru changes elements */
- for(i = 0; i < size-1 /* using nth and n+1 elements per loop */; i++){
- if(*(temp_ptr) > *(temp_ptr + 1)){ /* the next element is smaller than the previous */
- //printf("Old order: %ld\t%ld\n",*(ar_ptr),*(ar_ptr + 1));
- temp = *(temp_ptr);
- *(temp_ptr) = *(temp_ptr + 1);
- *(temp_ptr + 1) = temp;
- finished_sort_flag = false;
- //printf("New order: %ld\t%ld\n",*(ar_ptr),*(ar_ptr + 1));
- }
- //printf("old ar_ptr: %lp\n",temp_ptr);
- temp_ptr = temp_ptr + 1; /* pointer arithmetic */
- //printf("New ar_ptr: %lp\n",temp_ptr);
- }
- }
- /*
- for(i = 0; i < size; i++){
- //printf("%d\n",i);
- printf("Element %d: %ld\n",i,*(ar_ptr + i));
- }
- */
- //return ar_ptr;
- }
Add Comment
Please, Sign In to add comment