bubble sort final

 avatar
user_8306938
csharp
8 days ago
949 B
1
Indexable
Never
//by 吳聲寬 4112064029 電機一

#include <stdio.h>

void bubble(int *,int); //bubble sort function

int input[10000]={0};
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&input[i]); //scan all the insert value into array input
	}
	bubble(input,n);
	
	for(int i=0;i<n;i++){
		printf("%d ",input[i]); //print out all the value in array input
	}
	
	return 0;
}

void bubble(int *c,int n){
	if(n==1) return; //if there is only one value left, stop recusion
	int count=0; //this value is for checking if we did any switching
	for(int i=0;i<n-1;i++){
		if(c[i]>c[i+1]){
			int tmp=c[i];  //save value in tmp(temporary)
			c[i]=c[i+1];
			c[i+1]=tmp;
			count++;       //to know if we have ever switch
		}
		/*
			if left value larger than right value
			switch the value around
		*/
	}
	if(count==0) return; //if we did not do any switching, stop recusion
	bubble(c,n-1); //recusion
}
Leave a Comment