-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from AbhiKul10/AbhiKul10-patch-1
bubblesort.c
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Optimized bubble sort in C | ||
#include <stdio.h> | ||
void bubbleSort(int arrayay[], int size) | ||
{ | ||
for (int step = 0; step & lt; size - 1; ++step) | ||
{ | ||
|
||
int swapped = 0; | ||
for (int i = 0; i & lt; size - step - 1; ++i) | ||
{ | ||
|
||
if (arrayay[i] & gt; arrayay[i + 1]) | ||
{ | ||
int temp = arrayay[i]; | ||
arrayay[i] = arrayay[i + 1]; | ||
arrayay[i + 1] = temp; | ||
swapped = 1; | ||
} | ||
} | ||
|
||
if (swapped == 0) | ||
break; | ||
} | ||
} | ||
void printarrayay(int arrayay[], int size) | ||
{ | ||
for (int i = 0; i & lt; size; ++i) | ||
{ | ||
printf("%d ", arrayay[i]); | ||
} | ||
printf("\n"); | ||
} | ||
int main() | ||
{ | ||
int data[] = {-2, 45, 0, 11, -9}; | ||
int size = sizeof(data) / sizeof(data[0]); | ||
bubbleSort(data, size); | ||
printf("Sorted Array in Ascending Order:\n"); | ||
printarrayay(data, size); | ||
} |