User Tools

Site Tools


user:bkenne11:portfolio:bubsort

Project: BubbleSort

A project for Data Structures by Brandon Kennedy during the spring 2011 semester.

This project was begun on 11/18/2011 and is anticipated to take 32 minutes.

Objectives

The purpose of this project is to implement a bubble sort to introduce me to the concept of sorting algorithms. Through this project i hope to gain a better understanding of the Big 0 concept of the time taken to evaluate a sort.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • wiki page on bubble sorts.

Background

All i really want to get from this project is an understanding of set sorting algorithms. Why can't you just think up an algorithm on the spot? Because there are different speeds to each algorithm based on how you sort the data. This project will then sort via a bubble sort so i can see how long it takes to sort different quantities of data.

Scope

In this implementation i will build an array of integer values and sort them with an optimized bubble sort. The function will then print the values in the sorted order.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • Malloc: This program will malloc an array of integer pointers
  • Pointer: this program will use integer pointers.
  • Sorts: This program utilizes a bubble sort

Code

// bubble.c, a program by brandon kennedy for Data Structures
//in the spring 2011 semester
// implements a bubble sort
#include<stdio.h>
#include<stdlib.h>
 
int main()
{
        int i,input,c,swap,count;
        i=input=c=swap=count=0;
        int *list = (int*) malloc(sizeof(int)*64);
        printf("Please enter a positive integer value, (-1 to exit): ");
        scanf("%d", &input);
        while(input != -1)
        {
                *(list+i) = input;
                i++;
                count = i;
                printf("enter a value, (-1 to exit): ");
                scanf("%d", &input);
        }
        input = count - 1;
        for(i=0;i<=input;i++)
        {
                printf("[%d]: %d\n", i+1, *(list+i));
        }
        while(swap == 0)
        {
                swap=1;
                for(i=1;i<count;i++)
                {
                        if(*(list+(i-1)) > *(list+i))
                        {
                                c = *(list+(i-1));
                                *(list+(i-1))=*(list+i);
                                *(list+i) = c;
                                swap = 0;
                        }
                        }
                }
                count--;
        }
        for(i=0;i<=input;i++)
        {
                printf("[%d]: %d\n", i+1, *(list+i));
        }
        return 0;
}

Execution

lab46:~/src/DataS$ ./bubble
Please enter a positive integer value, (-1 to exit): 4
enter a value, (-1 to exit): 7
enter a value, (-1 to exit): 1
enter a value, (-1 to exit): 8
enter a value, (-1 to exit): 3
enter a value, (-1 to exit): 9
enter a value, (-1 to exit): -1
[1]: 4
[2]: 7
[3]: 1
[4]: 8
[5]: 3
[6]: 9
[1]: 1
[2]: 3
[3]: 4
[4]: 7
[5]: 8
[6]: 9

Reflection

There is definitely a benefit to using different sorting methods in different programs, and a benefit to having many different types documented. You can see what you need to sort, and pick an algorithm that fits your needs.

user/bkenne11/portfolio/bubsort.txt · Last modified: 2011/11/18 17:50 by bkenne11