Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions C/bubblesort.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
//code for bubble sort implementation

#include <stdio.h>

int main(void)
{
int Unsorted_Array[1000], size_of_array, Loop1_Counter, Loop2_Counter, swap_variable;

printf("Please enter the number of elements\n");
scanf("%d", &size_of_array);
printf("Enter the integers to be sorted\n");

for (Loop1_Counter = 0; Loop1_Counter < size_of_array; Loop1_Counter++)
scanf("%d", &Unsorted_Array[Loop1_Counter]); //input array

for (Loop1_Counter = 0 ; Loop1_Counter < size_of_array - 1; Loop1_Counter ++) //1st Loop for Counter
{
for (Loop2_Counter = 0 ; Loop1_Counter < size_of_array - Loop1_Counter - 1; Loop2_Counter ++) //2nd Loop for Swap
{
if (Unsorted_Array[Loop2_Counter] > Unsorted_Array[Loop2_Counter + 1]) // For descending order use <
{
swap_variable = Unsorted_Array[Loop2_Counter]; // code to swap values
Unsorted_Array[Loop2_Counter] = Unsorted_Array[Loop2_Counter + 1];
Unsorted_Array[Loop1_Counter + 1] = swap_variable;
}
}
}

printf("Sorted array in ascending order:\n");

for (Loop1_Counter = 0; Loop1_Counter < size_of_array; Loop1_Counter ++)
printf("%d\n", Unsorted_Array[Loop1_Counter]); // output sorted array

return 0;
}

int main() {
//code
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(a[j+1]<a[j]){
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
return 0;
}