如何在不使用循环的情况下进行排序

How to sort without using loop

本文关键字:情况下 排序 循环      更新时间:2023-10-16

在C++中如何在不使用循环的情况下对数组进行排序?我认为它可能使用递归,但我不知道如何实现它。

使用算法头文件中定义的std::sort

#include <iostream>
#include <algorithm>
using namespace std;
int main() 
{
    int a[]={5,3,4,1,2};
    sort(a,a+5);
    for(int i=0;i<5;i++)
        cout<<a[i]<<" ";         // 1 2 3 4 5
    return 0;
}
//C++
void bubblesort(vector<int> &arr, int iteration) {
    if(iteration == 0)
        return;
    bubbleswap(arr, 0, 1, iteration);
    bubblesort(arr, iteration-1);
}

void bubbleswap(vector<int> &arr, int i, int j, int n) {
    if(j>=n)
        return ;
    if(arr[i] < arr[j]){
        swap(arr[i], arr[j]);
    }
    bubbleswap(arr, i+1, j+1, n);
}

void sort(vector<int> &arr) {
    int n = arr.size();
    if(n<=1)
        return ;

    bubblesort(arr, n);
}

递归选择排序

#include <iostream>
#include <vector>
using namespace std;
//----------------------------------------------------------
int recursive_min(vector<int>& a, size_t first) {
   //base case
   if (first + 1 >= a.size()) {
      return first;
   }
   else {
      size_t min_index {first};
      int temp_index {recursive_min(a, first + 1)};
      if (a[min_index] > a[temp_index]) {
         min_index = temp_index;
      }
      return min_index;
   }
}
//----------------------------------------------------------
void selectionSort(vector<int>& a, size_t step = 0) {
   //base case
   if (step + 1 >= a.size()) {
      return;
   }
   else {
      int temp_index {recursive_min(a, step + 1)};
      if (a[step] > a[temp_index]) {
         swap(a[step], a[temp_index]);
      }
      selectionSort(a, step + 1);
   }
}
//---------------------------------------------------------
int main() {
   vector<int> vec {11, 23, 2, 50, 3, 8, 1, 4, 5};
   selectionSort(vec);
   for (int i : vec) {
      cout << i << " ";
   }
}

递归插入排序

#include <iostream>
#include <vector>
using namespace std;
int recursive_insert(vector<int>& a, int key, int i) {
   //base case
   if (i <= -1 || a[i] <= key) {
      return i;
   }
   else {
      a[i + 1] = a[i];
      return recursive_insert(a, key, i - 1);
   }
}
//-----------------------------------------------------------------------------
void insertion_sort(vector<int>& a, size_t j = 1) {
   //base case
   if (j >= a.size()) {
      return;
   }
   else {
      int key = a[j];
      int i = recursive_insert(a, a[j], j - 1);
      a[i + 1] = key;
      insertion_sort(a, j + 1);
   }
}
//-----------------------------------------------------------------------------
int main() {
   vector<int> vec {11, 23, 2, 50, 3, 8, 1, 4, 5};
   insertion_sort(vec);
   for (int i : vec) {
      cout << i << " ";
   }
}
#include<stdio.h>
int * sort(int *p,int i,int j,int size)
{
     if(i<size)
     { 
         if(j<size-i-1)
         {
             if(p[j]>p[j+1])
             {
                int temp = p[j];
                p[j] = p[j+1];
                p[j+1] = temp;
             }
        
    
         }
         else
         {
             j=-1;
             ++i;
         }
      p = sort(p,i,++j,size);
   }
   return p;
}

以下是如何使用

int  main()
{
    int array[] ={1,5,2,7,3};
    int len = sizeof(array)/sizeof(int);
    int *a = sort(array,0,0,len);
    for(int i=0;i<len;i++)
    {
       printf("n array[%d]->%d",i,a[i]);   
    }
}

我想您正在寻找快速排序算法

有更多的方法可以对数组进行排序。

如果你试图递归地实现一个循环,你可以看看维基百科上的一篇文章。这在"递归计算机科学"中有很好的解释。否则,您可以尝试实现不同的排序算法。众所周知的是Quicksort和Mergesort。有许多排序算法

好吧,我正在写一个Python解释器,还没有写循环。我从一些网站上得到了代码库,并通过一些递归重写了循环。它起作用D

def quickSort(arr, low, high):
    def jLoop(low, high, pivot, i, j):
        if j >= high:
            temp = arr[high]
            arr[high] = arr[i+1]
            arr[i+1] = temp
            return (i+1)
        else:
            if arr[j] <= pivot:
                i = i+1
                temp = arr[i]
                arr[i] = arr[j]
                arr[j] = temp
            return jLoop(low, high, pivot, i, j+1)
    def partition(low, high):
        i = (low - 1)
        pivot = arr[high]
        return jLoop(low, high, pivot, i, low)
    def quick(low, high):
        if low < high:
            pi = partition(low, high)
            quick(low, pi-1)
            quick(pi+1, high)
    quick(low, high)
    return arr
my_list = [0, 6, 3, 1, 2, 4, 7, 5]
length = 8
print quickSort(my_list, 0, 7)
my_list = [9, 0, 8, 1, 7, 2, 6, 3, 5, 4]
length = 10
print quickSort(my_list, 0, 9)
相关文章: