如何在不使用循环的情况下进行排序
How to sort without using loop
在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)
相关文章:
- 如何在不C++排序的情况下找到给定向量中的第一、第二和第三高值?
- C++数组与向量排序(在我的情况下,向量比数组慢~2.5倍(无优化))
- 类(可能是代理)的命名,允许在不修改基础容器的情况下对项目进行排序和删除
- c++ 选择排序在特定情况下不起作用
- 在不进行排序的情况下查找数组中n个最小值的索引
- 在最坏的情况下试验快速排序.它运行良好,但在最坏的情况下发生未知错误.我想
- 如何在不通过插入排序更改原始矢量的情况下对 2D 矢量进行排序
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- 如何在没有 c++ 中的数组/算法的情况下对两个条件的字符串进行排序
- 我可以使用 std::sort 对 C 样式数组(在我的结构情况下)进行排序吗?
- 特征在不复制的情况下对行重新排序
- 在不使用默认算法的情况下对字符串的 std 向量进行排序
- 在某些情况下,我的快速排序实现失败
- 在给定两个排序向量的情况下设置减法
- 在每种情况下都执行“std::set”对元素进行排序
- 如何在不使用循环的情况下进行排序
- 如何在只更改一个元素的情况下在排序列表中进行快速排序
- 在不知道输入大小的情况下对字符串 c++ 向量进行排序
- 在不使用数组的情况下对二进制文件中的数据进行排序
- 在不进行冒泡排序的情况下,计算数组中冒泡排序交换的次数