Search This Blog

Simplicity is the ultimate sophistication.” — Leonardo da Vinci
Contact me: sreramk360@gmail.com

Friday 27 March 2015

implementation of bubble sort algorithm in C



Implementation of bubble sort algorithm in C
 

/*
* implementation of bubble sort algorithm in C
* By K Sreram
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

enum BOOL {FALSE, TRUE};
typedef int BOOL;

struct NODE;
typedef struct NODE Node;
typedef Node* PtrNode;
typedef PtrNode List;
typedef PtrNode Position;
typedef int DataType;

struct NODE{ // single link list node
    DataType data;
    PtrNode next;
};

PtrNode NewList() // returns the List's header
{
    PtrNode tempNode =  malloc(sizeof(Node));
    tempNode->next = NULL;
    return tempNode;
}
Position NewNode() // creates a new node
{
    return malloc(sizeof(Node));
}

void Delete(PtrNode node)// deletes the node
{
    free(node);
}
BOOL IsLast(List L, Position pos) // checks id the given position is last in the list
{
    return (pos->next == NULL);
}


void Insert ( List L, DataType data, Position pos)// inserts a data field in a particular position
{
    Position tempPos;
    tempPos = NewNode(); // creates and allocates memory space
    if(tempPos == NULL)
    {
        fprintf(stderr, "error: request for allocation of memory using function \"malloc\" rejected");
        return;
    }
    tempPos->data = data;
    tempPos->next = pos->next;
    pos->next = tempPos;
}
void DeleteList(List L)
{
    PtrNode tempNode = L->next, tempNode2;
    while (tempNode != NULL)
    {
        tempNode2 = tempNode->next;
        Delete(tempNode);
        tempNode = tempNode2;
    }
    L->next = NULL;
}
void DisplayElement(DataType element) // displays an individual element
{
    printf("%d\n", element);
}
void Display (List L) // this function displays the list's contents linearly
{
    Position pos = L->next;
    if(pos == NULL)
        return;
    while (pos != NULL)
        {
            DisplayElement(pos->data);
            pos = pos->next;
        }
}
Position FindEnd(List L)
{

    Position tempPos=L;
    while(!IsLast(L, tempPos))
        tempPos = tempPos->next;
    return tempPos;
}
void SwapNodePair(PtrNode preNode) // swap the nodes connected to the passed on nodes
{
    Position temp = preNode->next;
    preNode->next = preNode->next->next;
    temp->next = preNode->next->next;
    preNode->next->next = temp;
}

void BubbleSort(List L) // this function runs at O(N^2) for the worse case
{
    if(L->next == NULL || L->next->next == NULL)
        return; // cannot sort 2 or one inputs
    Position pos;
    Position End = FindEnd(L);
    while(End != L->next)
    {
        pos = L;
        while(pos->next->next != End)
        {
            if(pos->next->data < pos->next->next->data)
                    SwapNodePair(pos);
            pos = pos->next;
        }
        if(pos->next->data < pos->next->next->data)
                    SwapNodePair(pos);
        End = pos->next;
    }
}

int main()
{
    List L = NewList(); // creates a new list
    int n, i, data;
    Position top = L;
    printf("enter size of list");
    scanf("%d", &n);
    if(n < 2 )
    {
        fprintf(stderr,"invalid list size. The size cannot be lesser than 2");
        getch();
        return -1;
    }
    printf("enter elements: ");
    for(i=0; i<n; i++)
    {
        scanf("%d", &data); // creates the list
        Insert(L, data, top);
        top = top->next;
    }
    BubbleSort(L);
    printf("\nthe sorted elements:\n");
    Display(L);
    getch();
    return 0;
}




copyright (c) 2015 K Sreram.
About my blog
 

No comments:

Post a Comment

Featured post

Why increasing complexity is not good?

“ Simplicity is the ultimate sophistication.” — Leonardo da Vinci Why is complicating things wrong ? - K Sr...