Brute Force and Optimization Algorithm Experiment: Topic 1 Sorting Problem Programming Integer Sorting Problem

Table of contents

foreword

1. Experimental content

2. The purpose of the experiment

3. Experimental steps

4. Experimental process

1. Algorithm analysis

2. Write pseudocode

3. Code implementation

4. Detailed code explanation

5. Use case testing

6. Complexity Analysis

Summarize


foreword

The integer sorting problem refers to the problem of rearranging a group of unordered integers according to certain rules (such as from small to large or from large to small). This is a classic algorithmic problem and there are many different solutions like bubble sort, selection sort, insertion sort, quick sort, merge sort, heap sort, etc. Different sorting algorithms have different time complexity and space complexity, as well as different stability and applicability. In this experiment, we will implement and compare several commonly used sorting algorithms, and analyze their advantages and disadvantages.

The language used in this article is C language

1. Experimental content

Input a sequence to be sorted, use the two sorting methods of selection sort and bubble sort to transform it into an ordered sequence, output the result, and require a text description when outputting. Please choose a language to write a program to implement the above algorithm, and analyze its algorithm complexity

2. The purpose of the experiment

(1) Master the basic ideas of selection sort and bubble sort;

(2) Master the specific implementation process of the two sorting methods;

(3) Program to realize two sorting methods on the basis of mastery.

3. Experimental steps

  1. According to the experimental content, the pseudo code of the algorithm is designed to describe the algorithm;

  2. Use C++/C/Java and other programming languages ​​to realize the engineering pseudo-code of the algorithm;

  3. Enter test cases to verify the algorithm;

  4. List the algorithm time complexity model and compare and analyze it with the computer running statistics time.

4. Experimental process

1. Algorithm analysis

Selection sort and bubble sort are two commonly used sorting algorithms. Their principles and performance have some similarities, but also some differences. This article will analyze their algorithms and compare their time complexity and space complexity.

The basic idea of ​​selection sorting is: select the smallest (or largest) element from the sequence to be sorted each time, and put it at the end of the sorted sequence until all elements are sorted. The time complexity of selection sort is O(n^2), because it needs to make n-1 comparisons, and each comparison needs to traverse ni elements, where i is the number of sorted elements. The space complexity of selection sort is O(1), because it only needs one extra variable to store the position of the smallest (or largest) element.

The basic idea of ​​bubble sorting is: each time starting from the head of the sequence to be sorted, compare two adjacent elements, and if the order is not correct, exchange their positions until all elements are sorted. The time complexity of bubble sorting is also O(n^2), because it needs n-1 traversals, and each traversal needs to compare ni elements, where i is the number of times it has been traversed. The space complexity of bubble sort is also O(1), because it only needs one extra variable to store the temporary value when swapping.

2. Write pseudocode

Bubble sort pseudocode: for i = 0 to n-1
    for j = 0 to ni-1
        if arr[j] > arr[j+1]
            swap(arr[j], arr[j+1])

Selection sort pseudocode: for i = 0 to n-1
    min_idx = i
    for j = i+1 to n
        if arr[j] < arr[min_idx]
            min_idx = j
    swap(arr[i], arr[min_idx])

3. Code implementation

#include <stdio.h>

void bubble_sort(int arr[], int n) {
   int i, j;
   for (i = 0; i < n-1; i++) {
       for (j = 0; j < n-i-1; j++) {
           if (arr[j] > arr[j+1]) {
               int temp = arr[j];
               arr[j] = arr[j+1];
               arr[j+1] = temp;
           }
       }
   }
}

void selection_sort(int arr[], int n) {
   int i, j;
   for (i = 0; i < n-1; i++) {
       int min_idx = i;
       for (j = i+1; j < n; j++) {
           if (arr[j] < arr[min_idx]) {
               min_idx = j;
           }
       }
       int temp = arr[i];
       arr[i] = arr[min_idx];
       arr[min_idx] = temp;
   }
}

int main() {
   int n;
   printf("请输入待排序序列的长度:");
   scanf("%d", &n);
   int arr[n];
   printf("请输入待排序序列:");
   for (int i=0; i<n; i++) {
       scanf("%d", &arr[i]);
   }

   printf("原始数组:");
   for (int i=0; i<n; i++) {
       printf("%d ", arr[i]);
   }
   printf("\n");

   bubble_sort(arr, n);
   printf("冒泡排序结果:");
   for (int i=0; i<n; i++) {
       printf("%d ", arr[i]);
   }
   printf("\n");

   selection_sort(arr, n);
   printf("选择排序结果:");
   for (int i=0; i<n; i++) {
       printf("%d ", arr[i]);
   }
   printf("\n");

   return 0;
}

4. Detailed code explanation

In this code, the bubble_sort function implements bubble sort, and the selection_sort function implements selection sort. In the main function, the user inputs the length of the sequence to be sorted and the sequence to be sorted itself. Then, both the original array and the two sorted results are printed.

Specifically, the bubble_sort function uses two nested loops to iterate over all elements in the array. The outer loop starts from the first element until the second-to-last element. The inner loop starts from the first element and goes to the end of the unsorted part. If two adjacent elements are out of order, swap them. In this way, the largest element will be placed at the end of the sorted part. Repeat this process until all elements are sorted.

The selection_sort function also uses two nested loops to iterate over all elements in the array. The outer loop starts from the first element until the second-to-last element. The inner loop starts with incrementing the outer loop variable by 1 until the end of the array. In the inner loop, find the smallest element in the unsorted part and store its index in the min_idx variable. This element is then swapped with the first element of the unsorted section. In this way, the smallest element will be placed at the end of the sorted part. Repeat this process until all elements are sorted.

Finally, in the main function, the user inputs the length of the sequence to be sorted and the sequence to be sorted itself. Then, both the original array and the two sorted results are printed.

5. Use case testing

6. Complexity Analysis

As mentioned above, the time complexity of selection sort is O(n^2), because it needs n-1 comparisons, and each comparison needs to traverse ni elements, where i is the number of sorted elements. The space complexity of selection sort is O(1), because it only needs one extra variable to store the position of the smallest (or largest) element. The time complexity of bubble sorting is also O(n^2), because it needs n-1 traversals, and each traversal needs to compare ni elements, where i is the number of times it has been traversed. The space complexity of bubble sort is also O(1), because it only needs one extra variable to store the temporary value when swapping.


Summarize

Bubble sort and selection sort are two simple sorting algorithms that can be used to sort arrays in C programming language. The main difference of bubble sort is that it operates by repeatedly swapping adjacent elements if they are not in the correct order whereas selection sort operates by repeatedly finding the smallest element from the unsorted part and placing it at the beginning of the array The array is sorted.

In terms of speed, the selection sort algorithm is faster than the bubble sort algorithm which is slower than the selection sort algorithm. Bubble sort needs n times, while selection sort needs n^2 times. Bubble sort is a stable algorithm, while selection sort is unstable. The selection sort algorithm is fast and efficient compared to the very slow and inefficient bubble sort.

 

Guess you like

Origin blog.csdn.net/m0_72471315/article/details/129987078