# Bubble Sort from any file in C

I have never heard of "Bubble sort" before so I went look up on Wikipedia under Pseudocode implementation.

``````procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
``````

This is for part of CS105 I am new a student. I have been stuck on this is third of my assignments that are due later today. I have finished the codes from my understanding try to use this pseudo-code convert to C program. I have completed the code so already. Can anyone help to fix this for me or what am I doing wrong? I am really hard to understand this "Bubble Sort" in C code.

``````/* 5 points */
#include <stdio.h>
#include <string.h>

#define MAXLEN 1000

/**
* Fill in the bubblesort function: https://en.wikipedia.org/wiki/Bubble_sort
* Using this pseudo-code in Wikipedia, convert to C code.
*/
void bubblesort (char *A[], int n) {
int i, j, temp;

for (i = 0; i < length; i++) {
for (j = 0; j < length - 1; j++) {
if (*A[j + 1] < *A[j]) {
temp = *A[j];
*A[j] = *A[j + 1];
*A[j + 1] = temp;
}
}
}
}

/**
* Create a main function that opens a file composed of words, one per line
* and sorts them using the bubble-sort algorithm.
* Usage: "Usage: %s <file>\n"
* Example input/output:
* ./p8 testfile
* Bob
* Bubbles
* Butters
* Dave
* ...
*/
int main (argc char, *argv[]) {
int intRC;
if (argc < 2) {
printf ("Usage: %s <file>\n");
intRC = -1;
} else {
/*This needs attention:
* argv[2] will contain a single string
* array[] is a character array not a float, atof tries to convert the string to a float
* the parameter passed to bubble_sort needs to be an array of pointers, not a single pointer
* What you need to do...
*   Try to read the file specified by argv[2] into a buffer (char*)
*   Split the buffer into an array of strings
*   Pass that array to your bubble sort routine
*   Implement lots of error checking:
*    Does the file exist?
*    Is there anything to process? (does the file contain anything to sort)
*/
char array[] = atof(argv[2]);
bubble_sort (array, 100);
intRC = 0;
while( intRC<intItems ) {
printf("%c\n", array[intRC]);
intRC++;
}
}
// prints print new lines from file
return intRC;
}
``````

This is what I used from file to test, but can be any else too that's why I used `atof(argv[2]);` to get any file.

``````Preview of `textfile`:

Bob
Tiffany
Steve
Jim
Lucy
Fred
George
Jill
Max
Butters
Randy
Dave
Bubbles
``````