简体   繁体   English

C-对字符串进行冒泡排序数组,然后应用于无符号int数组

[英]C - Bubble sort array of character strings, then apply to unsigned int array

The program is supposed to take a file, say data.dat, filled with a list of up to 320 words all less than or equal to 29 characters (30 with the null character) and add each of those words to a global array. 该程序应该获取一个文件,例如data.dat,其中填充了多达320个单词的列表,这些单词均小于或等于29个字符(其中30个为空字符),并将每个单词添加到全局数组中。 Then I want to sort that list alphabetically with a bubble sort. 然后,我想用冒泡排序按字母顺序对列表进行排序。 I did so with the following function in its own file sort.c 我这样做是在其自己的文件sort.c中使用以下功能

#include "set.h"
#include "sortAndSearch.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void bubbleSort(char A[][30], int num) {
int i, j;
char temp[30];

for(i = 0; i < num; i++)
    for(j = 0; j < num-1; j++)
        if(strcmp(A[i], A[i+1]) > 0){
            //swap the two array elements
            strcpy(temp, A[j]);
            strcpy(A[j], A[j+1]);
            strcpy(A[j+1], temp);
        }
}

I need a set of unsigned ints 我需要一组未签名的整数

unsigned int Set[10];

to act as an index for the names array. 作为名称数组的索引。 So each unsigned int has 32 bits and there are 10 unsigned ints for a total of 320 bits and each bit will reference a word. 因此,每个无符号整数都有32位,并且有10个无符号整数(总共320位),每个位将引用一个字。 I am unsure how to approach this part. 我不确定如何处理这一部分。

The end goal is to create functions to manipulate the sets. 最终目标是创建用于操作集合的函数。 I feel like I can attack that myself if I can get the sorting and index down, as I've done something similar but without using character arrays. 我觉得如果我可以减少排序和索引的话,我可以攻击自己,因为我做了类似的事情,但是没有使用字符数组。 The contents of the header file set.h that defines the functions to be used follows 定义要使用的功能的头文件set.h的内容如下

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

// defining new type(s)
typedef unsigned int Set[10];

// declaring global variable(s)
extern char names[320][30];

extern void setUnion(Set set1, Set set2, Set result);
extern void setIntersection(Set set1, Set set2, Set result);
extern void clearSet(Set set);
extern void add2Set(Set set, int value);
extern void deleteFromSet(Set set, int value);
extern int isMember(Set set, int element);
extern void printSet(Set);
extern int nameIsMember(Set set, char *);
extern void addName2Set(Set set, char *);
extern void deleteNameFromSet(Set set, char *);

And here are the contents of the header file sortAndSearch.h 这是头文件sortAndSearch.h的内容

void bubbleSort(char A[][30], int num);

int binarySearch(char A[][30], char *, int, int );

Your bubble sort is in fact not a bubble sort. 实际上,您的气泡排序不是气泡排序。 It only makes a single pass over the array. 它仅对数组进行一次传递。 You need to repeatedly pass over the array until you make a pass that does not result in a swap. 您需要反复遍历数组,直到进行不导致交换的遍历为止。 At that point you know that your array is ordered. 到那时,您知道阵列已排序。

My preferred bubble sort implementation has an outer do loop. 我首选的冒泡排序实现有一个外部do循环。 The inner loop is just as you currently have. 内部循环与您当前的状态相同。 The outer loop terminates when the latest inner loop execution failed to swap any items. 当最新的内部循环执行未能交换任何项目时,外部循环终止。

Of course, I'd recommend using a better sorting algorithm in the long run, but this is probably a homework assignment. 当然,从长远来看,我建议使用更好的排序算法,但这可能是一项家庭作业。

Problem is with this part: 问题出在这部分:

for(i = 0; i < num-1; i++)
        if(strcmp(A[i], A[i+1]) > 0){
            //swap the two array elements
            strcpy(temp, A[i]);
            strcpy(A[i], A[i+1]);
            strcpy(A[i+1], temp);
        }

There's supposed to be two loops for bubble sort! 冒泡排序应该有两个循环! ;-) See here for example with integers. ;-)例如,请参见此处的整数。

The bubblesort is not implemented correctly. Bubblesort执行不正确。 There should be an inner loop and an outer loop. 应该有一个内循环和一个外循环。

I am unclear about the purpose of a 320 bit set: It can't be used for sorting, or for much of anything other than each bit indicating some attribute of the array is true, like whether it is present, or contains a verb or something. 我不清楚320位集合的用途:它不能用于排序,或用于其他任何东西,除了每个位都表明数组的某些属性为真(例如它是否存在,是否包含动词或一些东西。

Would it be fair to summarise that the sorting of names is independent of the set operations? 总结名称排序独立于设置操作是否公平?

As long as the names don't change, the set operations will work? 只要名称不变,设置操作是否有效?

The operations you listed fall into these groups: 您列出的操作分为以下几类:

Straight set operations, it doesn't matter what they are a set of. 直集运算,不管它们是什么集合。 They operate on a whole set at a time: 它们一次在整个集合上运行:

extern void setUnion(Set set1, Set set2, Set result);
extern void setIntersection(Set set1, Set set2, Set result);
extern void clearSet(Set set);

You could tackle these without worrying about what the set might mean, but youu will need some values i there to be able to code them or use them. 您可以解决这些问题而不必担心集合可能意味着什么,但是您将需要一些值,以便能够对其进行编码或使用。

Set element operations, these manipulate a single bit in the set, and again don't much care what the bits represent: Set元素操作,这些操作集合中的单个位,并且再也不在乎这些位代表什么:

extern void add2Set(Set set, int value);
extern void deleteFromSet(Set set, int value);
extern int isMember(Set set, int element);

This is a good place to start. 这是一个很好的起点。

These operations map between the names and the set so need more stuff to work: 这些操作在名称和集合之间进行映射,因此需要更多工作:

extern int nameIsMember(Set set, char *);
extern void addName2Set(Set set, char *);
extern void deleteNameFromSet(Set set, char *);

I am not sure what extern void printSet(Set); 我不确定extern void printSet(Set); means, does it print the names which are in the set? 意思是,它会打印出集合中的名称吗?

Okay, a set is 好,一套是

unsigned int s[10];

to test a bit, we need to convert an int value to the index of an int, and the bit within the int. 为了测试一点,我们需要将一个int值转换为一个int的索引,以及该int内的位。 The simplest way is array index = (value/32), bit offset is (value%32). 最简单的方法是数组索引=(值/ 32),位偏移量是(值%32)。 The compiler should do a good job of making this efficient, so don't worry about efficiency. 编译器应该很好地做到这一点,所以不必担心效率。

To get at a bit value in s: (s[(value/32)] & (1<<(value%32)) 要获得s中的位值:(s [((value / 32)]&(1 <<(value%32))

So do you understand the bit operators ? 那么您了解位运算符吗?

~ not
& and
| or
^ xor
<< left shift
>> right shift

They are the operations you will use to set, clear and change bits in the set. 它们是您将用于设置,清除和更改集合中位的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM