我工作的一个项目,在C.课程
我写了这个代码基于冒泡排序算法,但如果我尝试许多数组进行排序它不工作:
void bubbleSort(int arraySize)
{
int tempEntry[6];
for (int i = 0; i < arraySize - 1; i++) //Loop for ascending ordering
{
for (int j = 0; j < arraySize - 1 - i; j++) //Loop for comparing other values
{
if (sortedArray[j][3] > sortedArray[j + 1][3]) //Comparing other array elements . n1>n2
{
copyTripleToArray(sortedArray[j], 0, 2, tempEntry); //Using temporary variable for storing last value
copyTripleToArray(sortedArray[j + 1], j, 0, NULL); //replacing value
copyTripleToArray(tempEntry, j + 1, 0, NULL); //storing last value
}
if (sortedArray[j][3] == sortedArray[j + 1][3] && sortedArray[j][4] > sortedArray[j + 1][4]) //Comparing other array elements. n1=n2, m1>m2
{
copyTripleToArray(sortedArray[j], 0, 2, tempEntry); //Using temporary variable for storing last value
copyTripleToArray(sortedArray[j + 1], j, 0, NULL); //replacing value
copyTripleToArray(tempEntry, j + 1, 0, NULL); //storing last value
}
}
}
}
copyTripleToArray()
的说明:
功能拷贝三重到合适的指数
sortedArray
(缓冲液= 0)时,或在outputBuffer
(缓冲= 1),或在tempArray
(缓冲= 2)。
void copyTripleToArray(int PrimitivePythagoreanTriple[], int index, int buffer, int tempArray[])
{
if (buffer == 1) //the array is outputBuffer
{
for (int i = 0; i < NUM_OF_ELENENTS_IN_ARRAY; i++) //first case: loop to copy all the entries
outputBuffer[index][i] = PrimitivePythagoreanTriple[i]; // copy the array to buffer.
return;
}
else if (buffer == 0) //the array is outputBuffer
{
for (int i = 0; i < NUM_OF_ELENENTS_IN_ARRAY; i++) //secound case: loop to copy all the entries into sortedArray
sortedArray[index][i] = PrimitivePythagoreanTriple[i]; // copy the array to sortedArray.
return;
}
for (int i = 0; i < NUM_OF_ELENENTS_IN_ARRAY; i++) //third case: loop to copy all the entries into tempArray
tempArray[i] = PrimitivePythagoreanTriple[i]; // copy the array to tempArray.
}
sortedArray
是排序的阵列;这是一个全球性的阵列。
copyTripleToArray
拷贝整数的数组,以sortedArray
或给临时数组的特定索引。
我究竟做错了什么?
这是很难分清这一点,因为它极为缺乏的MCVE(Minimal, Complete, Verifiable Example)的品质。使用全局变量数组排序是一个坏主意;传递数组排序功能。
你说:
......如果我尝试许多数组进行排序它不工作
你应该以什么方式这是行不通的解释。一个明显的问题是,你必须将数据复制到全球阵列每组数据进行排序。它的很多原因,你应该将数组传递给排序功能,而不是依赖于全局变量之一。对待全局变量谨慎。使用它们仅在替代(如传递参数)是太折磨人(和“痛苦”是最低水平的疼痛应该引起您使用全局变量,不要仅仅通过“不舒服”,甚至只是“痛苦”被推迟)。
花絮:“元素”有米的它 - 你的大长NUM_OF_ELENENTS_IN_ARRAY没有在正确的地方的M。然而,变量和常量名是安慰我太久。是;这是很好的选择有意义的名称。没有;没有必要写一个语法正确的文章入名称。我用NUM_VALUES
; NUM_COLUMNS
将工作太。他们是足够长的和有意义的。
更为严重的是,你有一个全局变量OutputBuffer
对此有任何解释 - 它是在copyTripleToArray()
引用,这是一个函数,副本6元一个奇特的名字。然而,细看之下表明,只有当buffer == 1
,这样的代码块可以被删除使用,但你永远不调用与buffer == 1
功能(注释)。
你copyTripleToArray()
功能是谦虚离奇。您应该简单地提供一个指向该行被复制出来,该行被复制并让它与复制。它是独特的,你需要这么多的参数。两个或三个应该是足够了(这取决于您是否传递NUM_VALUES
的功能或没有)。
然而,当一切都说过和做过,看来该代码工作。至少,我在下面的代码所做的修改应该是微不足道的。我创建并填充的数组,跑在上面的代码,它似乎产生正确的结果。
#include <stdio.h>
#include <stdlib.h>
enum { NUM_VALUES = 6 };
enum { NUM_OF_ELEMENTS_IN_ARRAY = NUM_VALUES };
static int sortedArray[][NUM_VALUES]; // Forward declaration
static void copyTripleToArray(int PrimitivePythagoreanTriple[], int index, int buffer, int tempArray[])
{
//if (buffer == 1)
//{
//for (int i = 0; i < NUM_OF_ELEMENTS_IN_ARRAY; i++)
//outputBuffer[index][i] = PrimitivePythagoreanTriple[i];
//}
//else if (buffer == 0)
if (buffer == 0)
{
for (int i = 0; i < NUM_OF_ELEMENTS_IN_ARRAY; i++)
sortedArray[index][i] = PrimitivePythagoreanTriple[i];
}
else
{
for (int i = 0; i < NUM_OF_ELEMENTS_IN_ARRAY; i++)
tempArray[i] = PrimitivePythagoreanTriple[i];
}
}
static void bubbleSort(int arraySize)
{
int tempEntry[NUM_VALUES];
for (int i = 0; i < arraySize - 1; i++)
{
for (int j = 0; j < arraySize - 1 - i; j++)
{
if (sortedArray[j][3] > sortedArray[j + 1][3])
{
copyTripleToArray(sortedArray[j], 0, 2, tempEntry);
copyTripleToArray(sortedArray[j + 1], j, 0, NULL);
copyTripleToArray(tempEntry, j + 1, 0, NULL);
}
if (sortedArray[j][3] == sortedArray[j + 1][3] &&
sortedArray[j][4] > sortedArray[j + 1][4])
{
copyTripleToArray(sortedArray[j], 0, 2, tempEntry);
copyTripleToArray(sortedArray[j + 1], j, 0, NULL);
copyTripleToArray(tempEntry, j + 1, 0, NULL);
}
}
}
}
static void print_array(const char *tag, size_t size, int data[size][NUM_VALUES])
{
printf("%s (%zux%d):\n", tag, size, NUM_VALUES);
for (size_t i = 0; i < size; i++)
{
printf("%3zu:", i);
for (int j = 0; j < NUM_VALUES; j++)
printf(" %3d", data[i][j]);
putchar('\n');
}
}
static int sortedArray[][NUM_VALUES] =
{
// random -n 30 -T '%d %d %d %d %d %d' 10 29 |
// commalist -n 6 -B 4 -b '{ ' -w -W 3 -T ' },'
{ 25, 18, 29, 25, 12, 18, },
{ 29, 29, 24, 23, 26, 28, },
{ 16, 22, 10, 15, 23, 29, },
{ 27, 22, 16, 27, 19, 24, },
{ 17, 18, 10, 20, 15, 24, },
{ 21, 11, 19, 15, 13, 15, },
{ 16, 11, 19, 13, 10, 25, },
{ 17, 17, 15, 27, 26, 24, },
{ 12, 23, 24, 28, 24, 15, },
{ 11, 21, 25, 15, 18, 25, },
{ 12, 14, 25, 11, 13, 29, },
{ 16, 12, 11, 21, 19, 28, },
{ 18, 16, 20, 17, 15, 11, },
{ 13, 18, 11, 23, 23, 18, },
{ 29, 16, 29, 10, 22, 28, },
{ 13, 15, 24, 24, 28, 26, },
{ 28, 26, 13, 27, 18, 27, },
{ 10, 29, 18, 15, 24, 29, },
{ 24, 24, 27, 24, 21, 12, },
{ 10, 28, 12, 11, 27, 25, },
{ 12, 21, 28, 27, 11, 14, },
{ 19, 17, 11, 18, 25, 23, },
{ 19, 21, 10, 21, 20, 22, },
{ 18, 29, 12, 15, 28, 22, },
{ 25, 16, 15, 23, 27, 21, },
{ 28, 16, 11, 10, 24, 23, },
{ 29, 19, 22, 20, 28, 27, },
{ 16, 21, 17, 16, 25, 15, },
{ 11, 23, 17, 19, 27, 13, },
{ 12, 15, 18, 16, 26, 14, },
};
enum { NUM_ROWS = sizeof(sortedArray) / sizeof(sortedArray[0]) };
int main(void)
{
print_array("Before", NUM_ROWS, sortedArray);
bubbleSort(NUM_ROWS);
print_array("After", NUM_ROWS, sortedArray);
return 0;
}
样品运行:
Before (30x6):
0: 25 18 29 25 12 18
1: 29 29 24 23 26 28
2: 16 22 10 15 23 29
3: 27 22 16 27 19 24
4: 17 18 10 20 15 24
5: 21 11 19 15 13 15
6: 16 11 19 13 10 25
7: 17 17 15 27 26 24
8: 12 23 24 28 24 15
9: 11 21 25 15 18 25
10: 12 14 25 11 13 29
11: 16 12 11 21 19 28
12: 18 16 20 17 15 11
13: 13 18 11 23 23 18
14: 29 16 29 10 22 28
15: 13 15 24 24 28 26
16: 28 26 13 27 18 27
17: 10 29 18 15 24 29
18: 24 24 27 24 21 12
19: 10 28 12 11 27 25
20: 12 21 28 27 11 14
21: 19 17 11 18 25 23
22: 19 21 10 21 20 22
23: 18 29 12 15 28 22
24: 25 16 15 23 27 21
25: 28 16 11 10 24 23
26: 29 19 22 20 28 27
27: 16 21 17 16 25 15
28: 11 23 17 19 27 13
29: 12 15 18 16 26 14
After (30x6):
0: 29 16 29 10 22 28
1: 28 16 11 10 24 23
2: 12 14 25 11 13 29
3: 10 28 12 11 27 25
4: 16 11 19 13 10 25
5: 21 11 19 15 13 15
6: 11 21 25 15 18 25
7: 16 22 10 15 23 29
8: 10 29 18 15 24 29
9: 18 29 12 15 28 22
10: 16 21 17 16 25 15
11: 12 15 18 16 26 14
12: 18 16 20 17 15 11
13: 19 17 11 18 25 23
14: 11 23 17 19 27 13
15: 17 18 10 20 15 24
16: 29 19 22 20 28 27
17: 16 12 11 21 19 28
18: 19 21 10 21 20 22
19: 13 18 11 23 23 18
20: 29 29 24 23 26 28
21: 25 16 15 23 27 21
22: 24 24 27 24 21 12
23: 13 15 24 24 28 26
24: 25 18 29 25 12 18
25: 12 21 28 27 11 14
26: 28 26 13 27 18 27
27: 27 22 16 27 19 24
28: 17 17 15 27 26 24
29: 12 23 24 28 24 15
所以,这是我不明白你的问题是什么。
下面的代码的简化版本,使用更简单的copy_row()
函数,这可能如果需要升级为使用memmove()
或memcpy()
代替具有在主体中的循环。
#include <stdio.h>
#include <stdlib.h>
enum { NUM_VALUES = 6 };
static int sortedArray[][NUM_VALUES]; // Forward declaration
static void copy_row(int source[NUM_VALUES], int target[NUM_VALUES])
{
for (int i = 0; i < NUM_VALUES; i++)
target[i] = source[i];
}
static void bubbleSort(int arraySize)
{
int tempEntry[6];
for (int i = 0; i < arraySize - 1; i++)
{
for (int j = 0; j < arraySize - 1 - i; j++)
{
if ((sortedArray[j][3] > sortedArray[j + 1][3]) ||
(sortedArray[j][3] == sortedArray[j + 1][3] &&
sortedArray[j][4] > sortedArray[j + 1][4]))
{
copy_row(sortedArray[j], tempEntry);
copy_row(sortedArray[j+1], sortedArray[j]);
copy_row(tempEntry, sortedArray[j+1]);
}
}
}
}
static void print_array(const char *tag, size_t size, int data[size][NUM_VALUES])
{
printf("%s (%zux%d):\n", tag, size, NUM_VALUES);
for (size_t i = 0; i < size; i++)
{
printf("%3zu:", i);
for (int j = 0; j < NUM_VALUES; j++)
printf(" %3d", data[i][j]);
putchar('\n');
}
}
static int sortedArray[][NUM_VALUES] =
{
// random -n 30 -T '%d %d %d %d %d %d' 10 29 |
// commalist -n 6 -B 4 -b '{ ' -w -W 3 -T ' },'
{ 25, 18, 29, 25, 12, 18, },
{ 29, 29, 24, 23, 26, 28, },
{ 16, 22, 10, 15, 23, 29, },
{ 27, 22, 16, 27, 19, 24, },
{ 17, 18, 10, 20, 15, 24, },
{ 21, 11, 19, 15, 13, 15, },
{ 16, 11, 19, 13, 10, 25, },
{ 17, 17, 15, 27, 26, 24, },
{ 12, 23, 24, 28, 24, 15, },
{ 11, 21, 25, 15, 18, 25, },
{ 12, 14, 25, 11, 13, 29, },
{ 16, 12, 11, 21, 19, 28, },
{ 18, 16, 20, 17, 15, 11, },
{ 13, 18, 11, 23, 23, 18, },
{ 29, 16, 29, 10, 22, 28, },
{ 13, 15, 24, 24, 28, 26, },
{ 28, 26, 13, 27, 18, 27, },
{ 10, 29, 18, 15, 24, 29, },
{ 24, 24, 27, 24, 21, 12, },
{ 10, 28, 12, 11, 27, 25, },
{ 12, 21, 28, 27, 11, 14, },
{ 19, 17, 11, 18, 25, 23, },
{ 19, 21, 10, 21, 20, 22, },
{ 18, 29, 12, 15, 28, 22, },
{ 25, 16, 15, 23, 27, 21, },
{ 28, 16, 11, 10, 24, 23, },
{ 29, 19, 22, 20, 28, 27, },
{ 16, 21, 17, 16, 25, 15, },
{ 11, 23, 17, 19, 27, 13, },
{ 12, 15, 18, 16, 26, 14, },
};
enum { NUM_ROWS = sizeof(sortedArray) / sizeof(sortedArray[0]) };
int main(void)
{
print_array("Before", NUM_ROWS, sortedArray);
bubbleSort(NUM_ROWS);
print_array("After", NUM_ROWS, sortedArray);
return 0;
}
具有相同输入数据(和打印代码等)时,其产生相同的答案。我还没有正式验证保护性能 - 这一切都存在于未排序的数据行是存在于排序的数据。
不难进一步修改此代码,以便要排序的数组传递到分选函数作为参数,而不是作为一个文件范围的变量。这是一个更强大,也更容易重用,代码的版本。
你可以在我的SOQ代码在GitHub(堆栈溢出问题)的存储库作为文件sort31.c
(从问题的代码),sort67.c
(传递数组作为参数排序)在sort89.c
子目录和src/so-5380-3837(上面的代码)。