如何使用冒泡排序或选择排序按降序对指针数组中的数组进行排序? [英] How can I sort arrays in a pointer array in a descending order using bubble sorts or selection sort?
问题描述
我正在一个项目中,该项目以几种不同的方式对指针数组中的数组进行排序,尽管我停留在一种排序方式上。以第一个数字指示其后的数字数量的方式构建数组。例如(3,0,23,1):此数组在第一个索引之后有3个数字)。我想按从最低到最高的顺序对数组进行排序,但我不想更改第一个索引,这意味着数组看起来像这样(3,0,1,23)。这些是数组和指针数组:
int arr1 [] = {3,9,6,7};
int arr2 [] = {2,5,5};
int arr3 [] = {0};
int arr4 [] = {1,6};
int arr5 [] = {4,5,6,2,1};
int * pArr [SIZE] = {arr1,arr2,arr3,arr4,arr5};
此代码用于排序功能
<$对于(i = 0; i< SIZE; i ++),p $ p>
对于(j = 1; j< pArr [i] [0] +1)
{
; j ++)
{
if(pArr [i] [j]< pArr [i] [j-1])$ b $ b {
temp = pArr [i] [j ];
pArr [i] [j] = pArr [i] [j-1];
pArr [i] [j-1] =温度;
}
}
}
我想只能使用冒泡排序或选择排序,因为我是编程新手,对其他排序方法不太了解。
在这里。
#include< stdio.h>
void bubble_sort(int a [],size_t n)
{
for(size_t last = n;!(n <2); n = last)
{
for(size_t i = last = 1; i< n; i ++)
{
if(a [i]< a [i-1])$ b $ b {
int tmp = a [i];
a [i] = a [i-1];
a [i-1] = tmp;
last = i;
}
}
}
}
void sort_multiple_arrays(int * a [],size_t n)
{
为(size_t i = 0; i {
bubble_sort(a [i] + 1,a [i] [0]);
}
}
int main(void)
{
int arr1 [] = {3,9,6,7};
int arr2 [] = {2,5,5};
int arr3 [] = {0};
int arr4 [] = {1,6};
int arr5 [] = {4,5,6,2,1};
int * parr [] = {arr1,arr2,arr3,arr4,arr5};
const size_t N = sizeof(parr)/ sizeof(* parr);
sort_multiple_arrays(parr,N);
for(size_t i = 0; i< N; i ++)
{
for(size_t j = 0; j {
printf(%d,parr [i] [j]);
}
putchar(’\n’);
}
返回0;
}
程序输出为
3 6 7 9
2 5 5
0
1 6
4 1 2 5 6
I am working on a project that sorts arrays inside the pointer arrays in a few different ways, though I am stuck on one way of sorting. The arrays are built in a way that the first number indicates the amount of numbers after it. For example, (3,0,23,1): this array has 3 numbers after the first index). I want to sort the array from lowest to highest number but I don't want to change the first index meaning the array will look like this (3,0,1,23). These are the arrays and the pointer array:
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int * pArr[SIZE] = { arr1, arr2, arr3, arr4, arr5 };
This code is for the sorting function
for (i = 0; i < SIZE; i++)
{
for (j = 1; j < pArr[i][0]+1; j++)
{
if (pArr[i][j] < pArr[i][j - 1])
{
temp = pArr[i][j];
pArr[i][j] = pArr[i][j - 1];
pArr[i][j - 1] = temp;
}
}
}
I would like to use bubble sort or selection sort only as I am new to programming and don't know too much about the other sorting methods.
Here you are.
#include <stdio.h>
void bubble_sort( int a[], size_t n )
{
for ( size_t last = n; !( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i] < a[i-1] )
{
int tmp = a[i];
a[i] = a[i-1];
a[i-1] = tmp;
last = i;
}
}
}
}
void sort_multiple_arrays( int * a[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
bubble_sort( a[i] + 1, a[i][0] );
}
}
int main(void)
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int * parr[] = { arr1, arr2, arr3, arr4, arr5 };
const size_t N = sizeof( parr ) / sizeof( *parr );
sort_multiple_arrays( parr, N );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < parr[i][0] + 1; j++ )
{
printf( "%d ", parr[i][j] );
}
putchar( '\n' );
}
return 0;
}
The program output is
3 6 7 9
2 5 5
0
1 6
4 1 2 5 6
这篇关于如何使用冒泡排序或选择排序按降序对指针数组中的数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!