User Tools

Site Tools


user:dschoeff:portfolio:bubblesort

Bubble Sorting Implementation

A project for Data Structures by Dave Schoeffler during the Fall 2011 semester.

This project was begun on November 18th and is anticipated to take 2 hours.

Objectives

This project will explore the sorting algorithm called the bubble sort. To do this I will create a program that uses a bubble sort to sort a list of ten integers. The point of doing this is to obtain an understanding of a simple bubble sort algorithm.

Prerequisites

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

Background

The purpose of this project it to implement a bubble sort algorithm.

The entire data structures course up to this point has been about data management. To do this we have stored data in different data structures. But what good is the data if we do not know how to sort it?

The bubble sort project will attempt to sort data that is stored in a data structure. This implementation will sort data that is stored in an array but this could easily be changed to sort data being stored in a linked list or something like that.

Scope

Once I write the heart of the bubble sort algorithm, I would like to then test it out. To do this, I will load ten different integer values entered by the user into an array. Then the bubble sort algorithm will sort through the array until it has been sorted. Once that is done the array will be displayed.

Attributes

The following attributes will be used in my program.

  • pointers: A pointer will be used to mark the beginning of the array
  • malloc/new: You must allocate memory for the array
  • sort: This program implements a bubble sorting algorithm

Code

bubble.c

/*************************************************
* Author: Dave Schoeffler                         *
* Purpose: This program implements a bubble sort  *
* algorithm. The program will accept as input ten *
* integers and then store them in a dynamic array.*
* It will then implement a bubble sort algorithm  *
* on that array and then sort them in accending   *
* order. It will then output the newly sorted     *
* array.                                          *
*                                                 *
* Compile with: gcc -o bubble bubble.c            *
*                                                 *
* Execute with: ./bubble                          *
*                                                 *
**************************************************/
 
#include <stdio.h>
#include <stdlib.h>
int main()
{
   int *array;
   array = (int*) malloc(sizeof(int)*10);
   int i = 0,input=0,junk;
   int swapped = 1, tmp;
   //loads ten values from the user into the array
   for(i = 0; i < 10; i++)
   {
      printf("enter 10 different integers: ");
      scanf("%d",&input);
      *(array+i) = input;
   }
   //meat of the bubble sort
   while( swapped == 1)
   {
      swapped = 0;
      for(i = 1; i < 10; i++)
      {
         if(*(array+(i-1)) > *(array+i))//this statements checks to see
         {                              //if the elements are out of order
            tmp = *(array+i);
            *(array+i) = *(array+(i-1));
            *(array+(i-1)) = tmp;
            swapped = 1;
         }
      }
   }
   //displays the array
   for(i = 0; i < 10; i++)
   {
      printf("[%d]: %d\n", i , *(array+i));
   }
 
   return (0);
}

Execution

In the execution of my code, I ran the program twice entering different values each time.

lab46:~/src/data/sorts$ ./bubble
enter 10 different integers: 90
enter 10 different integers: 89
enter 10 different integers: 78
enter 10 different integers: 67
enter 10 different integers: 56
enter 10 different integers: 45
enter 10 different integers: 34
enter 10 different integers: 2
enter 10 different integers: 3
enter 10 different integers: 12
[0]: 2
[1]: 3
[2]: 12
[3]: 34
[4]: 45
[5]: 56
[6]: 67
[7]: 78
[8]: 89
[9]: 90
lab46:~/src/data/sorts$ ./bubble
enter 10 different integers: 543
enter 10 different integers: 22222222
enter 10 different integers: 976545
enter 10 different integers: 23423
enter 10 different integers: 0
enter 10 different integers: 4
enter 10 different integers: 567
enter 10 different integers: 22
enter 10 different integers: 22
enter 10 different integers: 6666
[0]: 0
[1]: 4
[2]: 22
[3]: 22
[4]: 543
[5]: 567
[6]: 6666
[7]: 23423
[8]: 976545
[9]: 22222222
lab46:~/src/data/sorts$

Notice that both times it sorted the data completely and accurately.

Reflection

This being a shorter project I wasn't able to have the opportunity to research, explore, fail at certain things, etc… as compared to some other projects I have done in the past. But despite that, I feel I learned a lot of things about how the bubble sort algorithm. In my opinion it is one of the simpler sorting algorithms out there. Being simple though comes with its disadvantages(that being its worse case scenario performance). This means that it is not the most optimal way to sort data. I plan to explore alternative ways of sorting data in the future an then compare the performance on my own. I can say after this project I now have a good knowledge of the bubble sort algorithm.

References

In performing this project, the following resources were referenced:

user/dschoeff/portfolio/bubblesort.txt · Last modified: 2011/11/18 23:18 by dschoeff