I have been having this problem lately when I get to the swapping part of this code, so what this code does is inputs an array and sorts it using the bubble sort method. The text file that reads this in has 10 numbers and names. Like this:
John 1
Mark 2
Matthew 2
Luke 3
Issac 4
Kane 5
Ryan 7
Abel 2
Adam 9
Eve 10
However when it prints it out, it shows this:
John 1
Mark 2
Matthew 2
Abel 2
Abel 3
Abel 4
Abel 5
Abel 7
Adam 9
Eve 10
Sorry, the question is why is it repeating Abel, and what can i do to fix it?
void bubbleSort (Word q){
int last = 9;
int Swapped = 1;
int i = 0;
int m = 0;
char* temp = "Hello";
printf("Temp is: %s
", temp);
int tempA;
while (Swapped == 1){
Swapped = 0;
i = 1;
while (i < last){
printf("%d
", i);
if (q.data[i] > q.data[i+1]) {
printf("Hello: %d
", i);
//Copy Name of Element
temp = q.name[i];
strcpy(q.name[i], q.name[i+1]);
strcpy(q.name[i+1] , temp);
//Copy Data of corresponding element
tempA = q.data[i];
q.data[i] = q.data[i+1];
q.data[i+1] = tempA;
Swapped = 1;
}
i = i + 1;
}
last = last - 1;
}
last = 9;
while (m <= last){
printf("Name: %s, Data: %d
", q.name[m], q.data[m]);
m++;
}
}
See Question&Answers more detail:os