Hello,
I'm trying to write a program that sorts an array of ints.
I know i can easily find one on internet but I want to know why my program is not working.
Btw, in this exercice i don't want to use for loop, only while.
Code:
#include<unistd.h>
#include<stdio.h>
void sort(int *arr, int size);
int main(void)
{
int ptr[4]; //Initializing the value of the ints
ptr[0]=2;
ptr[1]=4;
ptr[2]=1;
ptr[3]=3;
sort(ptr,4); //calling the function
int i;
i=0;
while(i<=3)
{
printf("%i",ptr[i]); // Printing the modified ptr[]
i++;
}
}
void sort(int *arr, int size)
{
int b; //counter
b=0;
int temp; //temporary variable for storage
while(b<size)
{
if(arr[b]>arr[b+1])
{
temp=arr[b];
arr[b]=arr[b+1]; //swaping values
arr[b]=temp;
}
else
{
b++; //If int in order just add 1 to b
}
}
}