Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void print_array(int,int[]);
- void swap(int,int,int[]);
- int main(int argc, char const *argv[])
- {
- int input_array[] = {23,43,56,63,53,55,21,66,89,54,33};
- int input_array_size = sizeof(input_array)/sizeof(int);
- print_array(input_array_size,input_array);
- /*it is not necessary to go till the very last number because we are checking that in i+1 anyway*/
- for (int j = 0; j < input_array_size-1; j++){
- /*again we can skip the last j numbers because they are already sorted in the previous loop*/
- for(int i=0;i<input_array_size-j-1;i++){
- if(input_array[i]>input_array[i+1]){
- printf("checking %d and %d\n",input_array[i],input_array[i+1] );
- swap(i,i+1,input_array);
- }
- }
- }
- print_array(input_array_size,input_array);
- return 0;
- }
- void print_array(int size,int array[]){
- printf("\n");
- for(int i=0;i<size;i++){
- printf("[%d]",array[i] );
- }
- }
- void swap(int p1,int p2,int *array){
- int temp = *(array+p1); // gets the value at the pointer location and saves it to temp
- *(array+p1) = *(array+p2);
- *(array+p2) = temp;
- }
Add Comment
Please, Sign In to add comment