0% found this document useful (0 votes)
3 views1 page

C Sorting Algorithms: Bubble & Selection

The document provides an overview of two sorting algorithms implemented in C: Bubble Sort and Selection Sort. Each algorithm includes a step-by-step description and corresponding C code for sorting an array in ascending order. The Bubble Sort repeatedly compares and swaps adjacent elements, while the Selection Sort finds the minimum element and swaps it with the current position.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

C Sorting Algorithms: Bubble & Selection

The document provides an overview of two sorting algorithms implemented in C: Bubble Sort and Selection Sort. Each algorithm includes a step-by-step description and corresponding C code for sorting an array in ascending order. The Bubble Sort repeatedly compares and swaps adjacent elements, while the Selection Sort finds the minimum element and swaps it with the current position.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sorting Algorithms in C

1. Bubble Sort (Ascending Order)


Algorithm:
1. Start
2. Read size of array
3. Read array elements
4. Repeat for i = size-1 to 1
Compare adjacent elements
Swap if left element is greater
5. Print sorted array
6. Stop
C Code:
#include <stdio.h>
int main(){
int size,i,j,temp;
scanf("%d",&size;);
int arr[size];
for(i=0;i<size;i++) scanf("%d",&arr;[i]);
for(i=size-1;i>0;i--){
for(j=0;j<i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
return 0;
}

2. Selection Sort (Ascending Order)


Algorithm:
1. Start
2. Read size of array
3. Read array elements
4. For each position find minimum element
5. Swap minimum with current position
6. Print sorted array
7. Stop
C Code:
#include <stdio.h>
int main(){
int size,i,j,min,temp;
scanf("%d",&size;);
int arr[size];
for(i=0;i<size;i++) scanf("%d",&arr;[i]);
for(i=0;i<size-1;i++){
min=i;
for(j=i+1;j<size;j++){
if(arr[j]<arr[min]) min=j;
}
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
return 0;
}

You might also like