Skip to content

Instantly share code, notes, and snippets.

@X140Yu
Created February 15, 2016 13:56
Show Gist options
  • Save X140Yu/dbc1f237e82e3a36a911 to your computer and use it in GitHub Desktop.
Save X140Yu/dbc1f237e82e3a36a911 to your computer and use it in GitHub Desktop.
/*
* sort_lines.c
*
*
* Read in an array of lines from stdin, sort them ,
* and print the sorted strings back.
*/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_LINE_SIZE 100
#define INITIAL_BUFFER_SIZE 16
/*
* Input: @buffer: buffer to increse memory
* @buffer_size: the size of the buffer(pointer)
* Output: the double sized buffer
* Summary: This function doubles the capacity of your buffer by
* creating a new one and cleaning up the old one.
*/
char** IncreaseBuffer(char** buffer, int* buffer_size);
/*
* Input: @lines: lines to be printed
* @total_lines: total lines of the "lines""
* Summary: This function prints the lines in lines.
*/
void PrintLines(char** lines, int total_lines);
/*
* Input: @buffer: the buffer with lines in it
* @i: the location of a line
* @j: another location of a line
* Summary: This function gets the index of the minimum string,
* as determined by strcmp.
*/
int MinLineIndex(char** buffer, int i, int j);
/*
* Input: @buffer: buffer to swap
* @i: the location of a line
* @j: another location of a line
* Summary: This function swaps buffer[i] and buffer[j].
*/
void SwapLines(char** buffer, int i, int j);
int main(int argc, char *argv[])
{
// alloc memory
char **line_buffer = (char**)malloc(INITIAL_BUFFER_SIZE * sizeof(char*));
for(int i = 0; i < INITIAL_BUFFER_SIZE; i++) {
line_buffer[i] = (char *)malloc(MAX_LINE_SIZE * sizeof(char));
}
// get user input
int lines = 0;
int buffer_size = INITIAL_BUFFER_SIZE;
while(gets(line_buffer[lines++])) {
if(lines == buffer_size - 1) {
line_buffer = IncreaseBuffer(line_buffer, &buffer_size);
}
}
// sort lines using bubble sort
for(int i = 0; i < lines - 1; i++) {
for(int j = 0; j < lines - i - 1; j++) {
int min_index = MinLineIndex(line_buffer, j, j + 1);
if (min_index == j + 1) {
SwapLines(line_buffer, j, j + 1);
}
}
}
// print result
PrintLines(line_buffer, lines);
// free the memory
for(int i = 0; i < buffer_size; i++) {
free(line_buffer[i]);
}
free(line_buffer);
return 0;
}
char** IncreaseBuffer(char** buffer, int* buffer_size) {
// alloc memory for the new buffer
char **new_buffer = (char **)malloc(*buffer_size * 2 * sizeof(char*));
for(int i = 0; i < *buffer_size * 2; i++) {
new_buffer[i] = (char *)malloc(MAX_LINE_SIZE * sizeof(char));
}
// copy data
for(int i = 0; i < *buffer_size; i++) {
strcpy(new_buffer[i], buffer[i]);
}
// free the old space
for(int i = 0; i < *buffer_size; i++) {
free(buffer[i]);
}
free(buffer);
*buffer_size *= 2;
return new_buffer;
}
void PrintLines(char** lines, int total_lines)
{
for(int i = 1; i < total_lines; i++) {
printf("%s\n", lines[i]);
}
}
int MinLineIndex(char** buffer, int i, int j)
{
int ret = strcmp(buffer[i], buffer[j]);
if (ret < 0) {
return i;
} else {
return j;
}
}
void SwapLines(char** buffer, int i, int j)
{
char *temp = (char *)malloc((strlen(buffer[i]) + 1) * sizeof(char));
strcpy(temp, buffer[i]);
strcpy(buffer[i], buffer[j]);
strcpy(buffer[j], temp);
free(temp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment