Search This Blog

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

Friday, 27 March 2015

implementation of selection sorting algorithm in C



Implementation of selection sorting algorithm in C
 

/*
* implementation of selection sorting algorithm in C
*/

#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 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;
        }
}

void SwapNodes(PtrNode preNode1, PtrNode preNode2) // swap the nodes connected to the passed on nodes
{
    Position temp;
    temp = preNode1->next; // first swaps the nodes as such
    preNode1->next = preNode2->next;
    preNode2->next = temp;

    temp = preNode1->next->next; // now alter the swapped node's pointer links
    preNode1->next->next = preNode2->next->next;
    preNode2->next->next = temp;
}
void selectionSort(List L)
{
    if(L->next == NULL || L->next->next == NULL)
        return;
    Position pos = L;
    Position preMaxNode, preTempNode;
    while(pos != NULL)
    {
        preTempNode = pos;
        preMaxNode = pos;
        while(!IsLast(L, preTempNode))
        {
            if(preMaxNode->next->data < preTempNode->next->data)
                preMaxNode = preTempNode;
            preTempNode = preTempNode->next;
        }
        if(!IsLast(L, pos)) // if the top is the last data field, no swapping is needed
            SwapNodes(pos, preMaxNode);
        pos = 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;
    }
    selectionSort(L); // sorts the list
    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 complicating matters is not good

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