C language to implement bubble sort, selection sort, quick sort

1. Purpose:
Master the concept of algorithms and the problem-solving process.

2. Experiment content
Solve the sorting problem of n integers in many different ways.

3. Design and coding
1. Algorithm pseudocode
1.1 Bubble sort
Input: unordered array a[], number of array elements n
Output: ordered array a[n]
1. Define the initial value of exchange as n-1
2. When judging that the value of exchange is not 0
2.1 Set the maximum value bound to the value of exchange
2.2 If r[j]>r[j+1]
2.3 Exchange the exchange value with r[j]

1.2 Selection sorting
Input: unordered array a[], number of array elements n
Output: ordered array a[n]
1. Define the initial value of index as r[i], j=i+1
2. Judge r[j ]<r[index]
2.1 Exchange the values ​​of r[j] and r[index]

1.3 Quick sort
Input: unordered array a[], array first address number first, last address number end
Output: ordered array a[n]
1. Determine when the first address number first is less than the last address number end
1.1 If first<end && a[first]<=a[end], end–
1.1.1 Exchange the values ​​​​of a[first] and a[end], first++
1.2 If first<end && a[first]<=a[end], end++
1.2. 1 Exchange the values ​​​​of a[first] and a[end], first–

2. Program code
2.1 Bubble sort

#include<stdio.h>
void BubbleStort(int r[],int n)
{
    
    
	int bound,exchange=n-1;
	while(exchange!=0)
	{
    
    
		bound=exchange;
exchange=0;
		for(int j=0;j<bound;j++)
		if(r[j]>r[j+1])
		{
    
    
			int temp=r[j];
r[j]=r[j+1];
r[j+1]=temp;
			exchange=j;
		}
	}
}

2.2 Selection sorting

void SelectSort(int r[],int n)
{
    
    
	int i,j,index,temp;
	for(i=0;i<n-1;i++)
	{
    
    
		index=i;
		for(j=i+1;
j<n;j++)
		if(r[j]<r[index]) index=j;
		if(index!=i)
		{
    
    
			temp=r[i];
			r[i]=r[index];
			r[index]=temp;
		}
	}
}

2.3 Quick sort

int Partition(int r[],int first,int end)
{
    
    
	int i=first,j=end;
	while(i<j)
	{
    
    
		while(i<j&&r[i]<=r[j]) j--;
		if(i<j)
		{
    
    
			int temp=r[i];r[i]=r[j];r[j]=temp;
			i++;
		}
		while (i<j&&r[i]<=r[j]) i++;
		if(i<j)
		{
    
    
			int temp=r[i];r[i]=r[j];r[j]=temp;
			j--;
		}
	}
	return i;
	
}

PS: Recursive implementation

int Partition(int r[],int first,int end){
    
    
	int i=first,j=end;

	while(i<j){
    
    
		while(i<j&&r[i]<=r[j]) j--;
		if(i<j)
		{
    
    
			int temp=r[i];r[i]=r[j];r[j]=temp;
			i++;
		}

		while (i<j&&r[i]<=r[j]) i++;
		if(i<j){
    
    
			int temp=r[i];r[i]=r[j];r[j]=temp;
			j--;
		}
	}
	return i;	
}

void QuickSort(int r[],int first,int end){
    
    
	int pivot;
	if(first<end){
    
    
		pivot=Partition(r,first,end);
		QuickSort(r,first,pivot-1);
		QuickSort(r,pivot+1,end);
	}
}

int main(){
    
    
	int a[5]={
    
    7,5,9,8,2};
	printf("初始数组:7,5,9,8,2\n");
	QuickSort(a,7,2);
	putnum(a,5);
} 

2.4 Output (main function)

void putnum(int r[],int n)
{
    
    
	int i;
	printf("输出序列:");
	for(i=0;i<n;i++)
	printf("%d ",r[i]);
}
int main()
{
    
    
	int a[6]={
    
    30,8,7,45,5,24};
	printf("输入序列:30 8 7 45 5 24\n");
	BubbleStort(a,6); //冒泡排序
	putnum(a,6);
//	SelectSort(a,6); //选择排序
//	Partition(a,6,1); //快速排序
//	QuickSort(a,7,2); 
}

4. Operation results and analysis
1. Screenshot of results
Insert image description here

2. Analysis
We used bubble sort, selection sort and quick sort to successfully sort the given array. Each sorting method has its own advantages, and different sorting methods need to be used in different situations to maximize efficiency. The bubble sorting method is relatively simpler than the selection method. Since the bubble method needs to exchange the order of elements every time, the selection method only needs to record the subscript of the element, so the bubble method is less efficient. For arrays with smaller data, the bubble method is sufficient. For arrays with larger data, the efficiency of selection sorting will be significantly improved. The quick sort method, as the name suggests, is currently the fastest sorting method, but it is not as stable as the bubble sort method.

3. Time complexity calculation results
Insert image description here

5. Experiment summary
During the experiment, since the sorting knowledge has been learned in the data structure, there is no problem in most cases. The main problem arises from forgetting the knowledge of some functions such as parameter setting and calling, but through books and the Internet Tools can quickly recall learned knowledge.

Guess you like

Origin blog.csdn.net/qq_43605229/article/details/126351988