gpt4 book ai didi

c - 不同列长度的冒泡排序矩阵

转载 作者:行者123 更新时间:2023-11-30 19:47:04 28 4
gpt4 key购买 nike

我需要对具有不同列长度的矩阵进行冒泡排序,它需要不带 [] 操作数并且采用 C 语言。我尝试这样做:

for (i = 0; i < 5; i++)
{
first = (arr + i+d);// the first value in every line
for (j = 1; j <= (*first); j++)
{
for (d = 0; d <*(first)- j - 1; d++)
{
if (*(first+d+i) > *(first+d+1+i) )
{
swap =*(first+d);
*(first+d) = *(first+d+1);
*(first+d+1) = swap;
}
}
}
}

但是没有成功,有人可以帮助我吗?

最佳答案

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

typedef int Type;

#define PRN_Type "%d"

void print(Type **a, int size){
for(int i = 0;i<size;++i){
for(int j=1;j<=a[i][0];++j){
printf(PRN_Type " ", a[i][j]);
}
printf("\n");
}
}

void b_sort(Type **arr, int size){
int i, j, d;
Type swap, *first;
for (i = 0; i < size; ++i){
first = arr[i];//!!
for (j = 1; j <= *first; ++j){
for (d = 2; d <= *first -j+1; ++d){
if (*(first+d-1) > *(first+d) ){
swap =*(first+d-1);
*(first+d-1) = *(first+d);
*(first+d) = swap;
}
}
}
}
}

int main(void){
/*
int data[5][] = {
{3, 2,4,1},
{2, 99, 55},
{5, 9,5,1,7,5},
{1, 100},
{4, 5,5,5,5}
}
*/
int size = 5;
Type **data;
data = malloc(size * sizeof(Type*));
data[0]=(Type[]){3, 2,4,1};
data[1]=(Type[]){2, 99, 55};
data[2]=(Type[]){5, 9,5,1,7,5};
data[3]=(Type[]){1, 100};
data[4]=(Type[]){4, 5,5,5,5};
print(data, size);
b_sort(data, size);
printf("\n");
print(data, size);
return 0;
}

关于c - 不同列长度的冒泡排序矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728817/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com