The code of the sorting algorithm--selective sorting comprehensive training questions

Table of contents

Bubbling and rapid improvements? ? ?

Comprehensive application questions


Bubbling and rapid improvements? ? ?

Comprehensive application questions

1. Topic: Write a two-way bubbling algorithm , which scans alternately in the positive and negative directions, that is, the first pass puts the element with the largest keyword at the end of the sequence, and the second pass puts the element with the smallest keyword at the end of the sequence Foremost, so repeatedly.

Parse:

This sorting is called two-way bubbling. For an odd number of times, compare the keywords of adjacent elements from front to back, and exchange them when they encounter reverse order, until the element with the largest keyword in the sequence is moved to the end of the sequence. For an even number of times, compare the keywords of adjacent elements from the back to the front, and exchange when encountering a reverse order, until the element with the smallest keyword in the sequence is moved to the end of the sequence.

#include <stdio.h>
#include <stdbool.h>

typedef int ElemType;
ElemType a[50];//a[]待排序序列

void swap(int *a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

void BubbleSort(ElemType a[], int n)
{
	int low = 0, high = n-1,i;
	bool flag = true;
	
	while (low < high && flag)
	{
		
		flag = false;//每一趟开始之前都需要将标志归0.
		
		//奇数趟,正向走,将最大的移动到最后。
		for ( i = low; i < high; i++)
		{
			if (a[i] > a[i+1])
			{
				swap(&a[i],&a[i+1]);
				flag = true;//发生交换
			}	
		}
		high --;

		//偶数趟,倒向走,将最小的移动到最前面。
		for ( i = high; i > low; i--)
		{
			if (a[i] < a[i-1])
			{
				swap(&a[i],&a[i-1]);
				flag = true;
			}
		}
		low ++;
	}	
}

int main()
{
	int n;
	printf("请输入序列个数:\n");
	scanf("%d",&n);
	printf("请输入序列中的元素: \n");
	for (int i = 0; i < n; i++)
	{
		scanf("%d",&a[i]);
	}
	
	BubbleSort(a,n);
	printf("排序好的序列是:\n");
	for (int i = 0; i < n; i++)
	{ 
		printf("%d,",a[i]);
	}
	printf("\n");
	return 0;
}

result:

Please enter the number of sequences:
10
Please enter the elements in the sequence: 
12 13 39 43 10 33 23 90 82 19
The sorted sequence is:
10,12,13,19,23,33,39,43,82,90

Inadequacies in the process of knocking by yourself:

1. I didn't think that after two-way bubbling, the starting point and ending point of each sorting will change, and low and high should be added. Still thinking stupidly. . . waste. . .

2. Are you a silly bear? Why don't you think about whether to judge the situation of crossing the boundary, etc., only know the process of writing sorting, have you never thought about what the end condition is?

I hope you can remember! ! ! Judge the end condition! ! !

3. So how is the end of sorting? Of course, the flag is used to mark whether to sort each time, and if it is sorted, the flag is changed.

When defining bool flag = true, I initially defined it as flag = flase, and then,

Because the while is only executed when it is true, then I can only put the judgment of the flag in the while loop.

Because after each sorting, flag = true, so add a judgment at the beginning of the while loop: if flag = false, it means that there is no exchange right? Then break out of the loop.

There is a consequence: is it stupid, is it stupid, at the beginning you stipulated that flag = false! ! ! . So don't execute it again, just jump out. . .

After I said how to sort it did not change. . . Harmful… …

2. Knowing that the linear table is stored in order, and each element is a different integer element, design an algorithm to move all odd numbers to the front of all even numbers (requires the least time and the least auxiliary space).

Analysis: My opinion: Nothing new. The point is to require the least amount of time, and the least amount of auxiliary space. That is to say, the number of loop nesting layers should be less, and try to move on the original sequence. Wang Dao: This question can be designed based on the division idea of ​​quick sort: only need to traverse once. The time complexity is O(n), and the space complexity is O(1). The basic idea: first find an even element L(i) from the front to the back, then find an odd element L(i) from the back to the front, and combine the two Exchange; repeat the above process until low>high. (not exactly using quicksort).

#include <stdio.h>

typedef int ElemType;
ElemType a[50];

void swap(int *a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

void move(ElemType a[],int n)
{
	int low = 0, high = n-1;
	while (low < high)
	{
		while (low < high && a[low] % 2 != 0)
		{
			//只要是奇数就跳过,直到找到一个偶数,跳出while循环。
			low ++;
		}
		while (low < high && a[high] % 2 != 1)
		{
			//只要是偶数就跳过,直到找到一个奇数,跳出while循环。
			high --;
		}
		if (low < high)
		{
			swap(&a[low], &a[high]);//交换。
		}
		low ++; high --;//我居然把这一条件给忘了,不加这一句,i和j的值就一直不变,就会死循环。
	}
}

int main()
{
	int n;
	printf("请输入序列个数:\n");
	scanf("%d",&n);
	printf("请输入序列中的元素: \n");
	for (int i = 0; i < n; i++)
	{
		scanf("%d",&a[i]);
	}
	
	move(a,n);
	printf("排序好的序列是:\n");
	for (int i = 0; i < n; i++)
	{ 
		printf("%d,",a[i]);
	}
	printf("\n");
	return 0;
}

To sum up: 1. The main function seems to be almost the same, so I won’t write it in the future. 2. In the exam, if you encounter the least time required, the best algorithm, etc., or there is no hint and it is not super suitable for other sorting algorithms, then start with quick sorting.

3. Try to rewrite the division algorithm of quick sort in the detailed analysis of test points, so that the pivot value selected each time is randomly selected from the current sub-table.

pass it? Use a random number to select the pivot. However, the selected random number must be exchanged with the first element, and the pivot is still = a[low].

4. Try to write an algorithm so that it can find the kth smallest element in the array L[1...n] (that is, the element at the kth position after sorting from small to large).

Parse:

My idea is to sort the array directly, and then extract L(k), and then get the kth smallest. However, in this way its average time complexity will reach more than O(nlog2n).

Wang Dao: You can use the idea of ​​small root piles, and the time complexity is O(n+klog2n).

The most exciting thing is the division operation based on quick sort.

The main idea:

5. The problem of the national flag of the Netherlands: There is a block sequence composed of only red, white, and blue blocks. Please write an algorithm with a time complexity of Q ( n ) Q(n) Q(n), These bars are arranged in the order of red, white and blue, that is, the pattern of the Dutch flag is arranged.
6. [2016 Unified Examination Questions] It is known that the set A = {ak ∣ 0 ≤ k < n} composed of n (n≥2) positive integers is divided into two disjoint subsets A1 and A2, The number of elements is n1 and n2 respectively, and the sum of the elements in A1 and A2 is S1 and S2 respectively. Design a partition algorithm that is as efficient as possible so that |n1 − n2| is minimum and |S1 − S2| is maximum.
Require:

1. Give the basic design idea of ​​the algorithm.

2. According to the design idea, use C language to describe the algorithm, and give notes on the key points.

3. Explain the average time complexity and space complexity of the algorithm you designed.

Guess you like

Origin blog.csdn.net/weixin_48060069/article/details/127156175