0% found this document useful (0 votes)
86 views

Data Structures

The document contains source code for bubble sort, insertion sort, selection sort, and quicksort algorithms implemented in C programming language to sort arrays in ascending order. Each algorithm section includes the code to input array size and elements, perform the sorting, and output the sorted array.

Uploaded by

Rizwan Khan
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)
86 views

Data Structures

The document contains source code for bubble sort, insertion sort, selection sort, and quicksort algorithms implemented in C programming language to sort arrays in ascending order. Each algorithm section includes the code to input array size and elements, perform the sorting, and output the sorted array.

Uploaded by

Rizwan Khan
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
You are on page 1/ 4

Source code of simple bubble sort implementation using array ascending order in c programming

language
#include<stdio.h>
int main(){
int s,temp,i,j,a[20];
printf(!nter total num"ers of elements# );
scanf($d,%s);
printf(!nter $d elements# ,s);
for(i&0;i<s;i'')
scanf($d,%a[i]);
(()u""le sortin* al*orithm
for(i&s+2;i>&0;i++){
for(j&0;j<&i;j''){
if(a[j]>a[j',]){
temp&a[j];
a[j]&a[j',];
a[j',]&temp;
-
-
-
printf(.fter sortin*# );
for(i&0;i<s;i'')
printf( $d,a[i]);
return 0;
-
/utput#
!nter total num"ers of elements# 0
!nter 0 elements# 1 2 0 ,, 2
.fter sortin*# 0 2 1 2 ,,
Source code of simple insertion sort implementation using array in
ascending order in c programming language
#include<stdio.h>
int main(){
int i,j,s,temp,a[20];
printf("nter total elements" ");
scanf("#d",$s);
printf("nter #d elements" ",s);
for(i%0;i<s;i&&)
scanf("#d",$a[i]);
for(i%';i<s;i&&){
temp%a[i];
j%i(';
)hile((temp<a[j])$$(j>%0)){
a[j&']%a[j];
j%j(';
*
a[j&']%temp;
*
printf("+fter sortin," ");
for(i%0;i<s;i&&)
printf(" #d",a[i]);
return 0;
*
-utput"
nter total elements" .
nter . elements" / 0 1 0 2
+fter sortin," 0 2 / 0 1
Source code of simple Selection sort implementation using array ascending order in c programming
language
#include<stdio.h>
int main(){
int s,i,j,temp,a[20];
printf(!nter total elements# );
scanf($d,%s);
printf(!nter $d elements# ,s);
for(i&0;i<s;i'')
scanf($d,%a[i]);
for(i&0;i<s;i''){
for(j&i',;j<s;j''){
if(a[i]>a[j]){
temp&a[i];
a[i]&a[j];
a[j]&temp;
-
-
-
printf(.fter sortin* is# );
for(i&0;i<s;i'')
printf( $d,a[i]);
return 0;
-
/utput#
!nter total elements# 0
!nter 0 elements# 3 0 0 2, 4
5he arra6 after sortin* is# 0 3 0 4 2,
Source code of simple quick sort implementation using array ascending order in c programming
language
#include<stdio.h>
7oid 8uic9sort(int [,0],int,int);
int main(){
int :[20],si;e,i;
printf(!nter si;e of the arra6# );
scanf($d,%si;e);
printf(!nter $d elements# ,si;e);
for(i&0;i<si;e;i'')
scanf($d,%:[i]);
8uic9sort(:,0,si;e+,);
printf(<orted elements# );
for(i&0;i<si;e;i'')
printf( $d,:[i]);
return 0;
-
7oid 8uic9sort(int :[,0],int first,int last){
int pi7ot,j,temp,i;
if(first<last){
pi7ot&first;
i&first;
j&last;
=hile(i<j){
=hile(:[i]<&:[pi7ot]%%i<last)
i'';
=hile(:[j]>:[pi7ot])
j++;
if(i<j){
temp&:[i];
:[i]&:[j];
:[j]&temp;
-
-
temp&:[pi7ot];
:[pi7ot]&:[j];
:[j]&temp;
8uic9sort(:,first,j+,);
8uic9sort(:,j',,last);
-
-
/utput#
!nter si;e of the arra6# 0
!nter 0 elements# > ? 0 , 2
<orted elements# 0 , 2 > ?

You might also like