C language educator sorting algorithm

This article is for reference only!!!!!!

Bubble sort algorithm

Title Description: bubble sorting repeatedly visits the element column to be sorted, compares two adjacent elements in turn, and exchanges them if the order (such as from large to small, and the initial letter from Z to A) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is, the element column has been sorted. The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence (in ascending or descending order) through exchange, just as the bubbles of carbon dioxide in carbonated drinks will eventually float to the top, so it is called "bubble sorting".

#include <stdio.h>
#include <stdlib.h>

//Output array elements
void print(int data[] ,int n)
{
 for(int i=0;i<n;i++)
   printf("%d ",data[i]);
 printf("\n");  
}


/*First pass
 Please fill in the code here to realize incremental order and bubble sorting,
 After each sorting request, the print function is called to output the arrangement of keywords.*/
void bubbleSort( int data[] ,int n )
{
  
 /*----begin------*/
 int t;
  for(int i=0;i<n-1;i++){     
        for(int j=0;j<n-1-i;j++)//n-1-i comparisons per trip
        {
            if(data[j]>data[j+1]){ //Comparison of two adjacent numbers
            t=data[j];
            data[j]=data[j+1];
            data[j+1]=t;   
        }
          
        }
         print(data,n);
      
    }
}
 
 /*-----end------*/




Select Sorting Algorithm

Topic Description: Selection sort is a simple and intuitive sorting algorithm. Its working principle is: select the smallest (or largest) element from the data elements to be sorted for the first time, store it at the beginning of the sequence, then find the smallest (large) element from the remaining unordered elements, and then put it at the end of the sorted sequence. And so on until the number of all data elements to be sorted is zero.

/*Second pass
 Please fill in the code here to select and sort in ascending order,
 After each sorting request, the print function is called to output the arrangement of keywords.*/
void selectSort( int data[] ,int n )
{
  
 /*----begin------*/
int min;//Define a variable to store the minimum subscript
 for(int i=0;i<n-1;i++)//The comparison times of external circulation control are n-1 times when there are n numbers
 {
   min=i;// Assume that the minimum value is data[i], that is, data[0]
   for(int j=i+1;j<n;j++)//Compare the minimum value with the number data[i+1]~data[n-1] after the minimum value
   {
   	  //Among these numbers, the minimum value is found by comparing with the specified minimum value data[min]
      if(data[j]<data[min])
      {
      	//If there is something smaller than the minimum
        min=j;//Record the subscript of the minimum value
      }
   }
   //Exit the inner loop and judge whether there is a value smaller than the minimum value
   //If so, exchange
   if(data[i]!=data[min])
   {
   	  //This is not a way of borrowing third-party variable exchange
      data[i]=data[min]+data[i];
      data[min]=data[i]-data[min];
      data[i]=data[i]-data[min];
   }
  print(data,n);
 }
}

Direct insert sort

Title Description: Straight Insertion Sort is the simplest sort method. Its basic operation is to insert a record into the arranged ordered table, so as to obtain a new ordered table with an increase of 1 in the number of records.

/*Third pass
 Please fill in the code here to realize the incremental order for direct insertion sorting,
 After each sorting request, the print function is called to output the arrangement of keywords.*/
void insertSort( int data[] ,int n )
{
  
 /*----begin------*/
 int i, j;
	int temp;	// Used to store temporary variables
 
	for(i = 1; i < n; i++)
	{
		temp = data[i];
		for(j = i-1; (j >= 0)&&(data[j] > temp); j--)
		{
			data[j + 1] = data[j];
		}
		data[j + 1] = temp;
    print(data,n);
	}

 
 /*-----end------*/

}

Binary insertion sort

Topic Description: binary insertion sort is also a kind of insertion sort algorithm. Its basic idea is to introduce the idea of binary search to reduce the number of comparisons on the basis of direct insertion sort, so as to find the insertion position faster.

/*The fourth level
 Please fill in the code here to realize incremental order and binary insertion sort,
 
  The essence is to use dichotomy to find the insertion position in the ordered table
  After each sorting request, the print function is called to output the arrangement of keywords.*/
void binInsertSort(  int data[] ,int n )
{
  
 /*----begin------*/
 
int i, j;
    for( i = 1; i < n; i++ )
    {
        if( data[i] < data[i - 1] )
        {
            int temp = data[i];
            int left = 0, right = i - 1;
            while( left <= right )
            {
                int mid = ( left + right ) / 2;
                if( data[mid] < temp )
                {
                    left = mid + 1;
                }
                else
                {
                    right = mid - 1;
                }
            }
            for( j = i; j > left; j-- )
            {
                data[j] = data[j - 1];
            }
            data[left] = temp;
        }
        print(data,n);
    }

 
 /*-----end------*/

Tags: C Algorithm data structure

Posted on Sat, 06 Nov 2021 11:12:59 -0400 by dror_israel