Divide and conquer algorithm to solve problems (c language)


foreword

Solve the following two problems:
1. Realize the quick sorting (ascending order) of 10 data;
2. There is an array A[10], which stores 10 integers, and the order is increasing. Enter a number n at will, and find the position of n in the array. If n does not belong to the array A, display an error message. A[10]={2,3,5,7,9,11,12,15,19,22}.


1. Question 1: Experimental procedures and results


#include <stdio.h>
void display(int array[], int maxlen)
{
    
    
    int i;
 
    for(i = 0;i<maxlen;i++)
    {
    
    
        printf("%5d",array[i]);
    }
    printf("\n");
 
    return ;
}
void display2(int array[], int maxlen)
{
    
    
    int i;
 
    for(i=9;i>-1;i--)
    {
    
    
        printf("%5d",array[i]);
    }
    printf("\n");
 
    return ;
}
void QuickSort(int *arr, int low, int high)
{
    
    
    if (low < high)
    {
    
    
        int i = low;
        int j = high;
        int k = arr[low];
        while (i < j)
        {
    
    
            while(i < j && arr[j] >= k) 
            {
    
    
                j--;
            }
 
            if(i < j)
            {
    
    
                arr[i++] = arr[j];
            }
 
            while(i < j && arr[i] < k)    
            {
    
    
                i++;
            }
 
            if(i < j)
            {
    
    
                arr[j--] = arr[i];
            }
        }
       arr[i] = k;
       QuickSort(arr, low, i - 1);    
       QuickSort(arr, i + 1, high);    
    }
}

int main()
{
    
       int i,c;
    int array[10];
	 printf("请输入十个数将进行升序\n");
	for(i=0;i<10;i++){
    
    
		
		printf("你输入的第%d个数是  ",i+1);
		scanf("%d",&array[i]);}
	printf("排序前的数组\n");
    display(array,10);
    QuickSort(array, 0, 9);
	printf("想要升序排列请按1\n想要降序排列请按2\n");
	scanf("%d",&c);
	if(c==1)
    {
    
    printf("排序后的数组(升序)\n");
	display(array, 10);}
	else if(c==2)
	{
    
    printf("排序后的数组(降序)\n");
	display2(array, 10);}
	else
	{
    
    printf("请正确输入,亲");}
    return 0;
}

Realize the effect:
insert image description here
insert image description here
insert image description here

Explanation of ideas:
1. Construct a display function for the final output and the initial output
2. Create an array to store the values ​​to be sorted 3. Create a
quicksort function and pass in the end value, start value, and array of three values
By comparing the subscripts that constantly change the position of the array value, and using recursion to continuously exchange
4. Ascending and descending output is the way to change the output of the array.

2. Question 2: Experimental procedures and results

#include <stdio.h>
#include <conio.h>
void QuickSort(int *arr, int low, int high)
{
    
    
    if (low < high)
    {
    
    
        int i = low;
        int j = high;
        int k = arr[low];
        while (i < j)
        {
    
    
            while(i < j && arr[j] >= k) 
            {
    
    
                j--;
            }
 
            if(i < j)
            {
    
    
                arr[i++] = arr[j];
            }
 
            while(i < j && arr[i] < k)    
            {
    
    
                i++;
            }
 
            if(i < j)
            {
    
    
                arr[j--] = arr[i];
            }
        }
       arr[i] = k;
       QuickSort(arr, low, i - 1);    
       QuickSort(arr, i + 1, high);    
    }
}
int main(){
    
    
  int a[10];
  int i,num,n,low,high,mid,m;
  printf("请输入十个数的数组:\n");
  for(i=0;i<10;i++){
    
    
	  printf("请输入第%d个数 ",i+1);
	  scanf("%d",&a[i]);
	 }
  printf("你输入的数组是\n");
  for(m=0;m<10;m++){
    
    
	  printf("%5d",a[m]);
	 }
   QuickSort(a, 0, 9);
  printf("\n从小到大排序后的数组是\n");
  for(m=0;m<10;m++){
    
    
	  printf("%5d",a[m]);
	 }
  printf("\n请输入数组中的一个数将查验此数位置:");
  scanf("%d",&num);
  n=10;
  low=0;
  high=n-1;
  while(low<=high){
    
    
    mid=(low+high)/2;
    if(num<a[mid])
      high=mid-1;
    else if(num>a[mid])
      low=mid+1;
    else if(num==a[mid]){
    
    
      printf("亲您选择的**%d**是数组中**第%d个**元素的值",num,mid+1);
      break;
    }
  }
  if(num!=a[mid])
    printf("无此数");
  getch();
  return 0;
}

Running results:
insert image description here
Explanation of ideas:
The second question can be regarded as an analogy to the first question, but there is an extra subscript idea to find the value of the element.

Guess you like

Origin blog.csdn.net/weixin_51759592/article/details/125766966