Ad Code

Responsive Advertisement

Data Structure assignment 02 : Sorting Techniques (Non Recursive)

 Practice Programs:

 download source code file click

 1) Write a C program to create a integer array with elements {56,23,11,67,12,89,2} and sort the given array using bubble sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

void bubbleSort(int arr[], int n);

 

int main()

{

    int arr[] = {56, 23, 11, 67, 12, 89, 2};

    int n = sizeof(arr) / sizeof(arr[0]);

    bubbleSort(arr, n);

 

    for (int i = 0; i < n; i++)

    {

        printf("%d\t", arr[i]);

    }

 

    getch();

    return 0;

}

 

void bubbleSort(int arr[], int n)

{

    int i, j, temp;

    for (i = 1; i < n; i++)

    {

        for (j = 0; j < n - i; j++)

        {

            if (arr[j] > arr[j + 1])

            {

                temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

    }

}

============================================================

2) Write a C program to sort a random array of n integers (value of n accepted from user) by using Bubble Sort / Insertion Sort algorithm in ascending order.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 void insertionSort(int arr[], int n);

 int main()

{

    int a[20], i, n;

     printf("Enter the size of Array  : ");

    scanf("%d", &n);

     printf("Enter the  array element : \n");

     for (i = 0; i < n; i++)

    {

        printf("Array index[%d] : ", i);

        scanf("%d", &a[i]);

    }

     insertionSort(a, n);

     for (i = 0; i < n; i++)

    {

        printf("%d\t", a[i]);

    }

     getch();

    return 0;

}

 void insertionSort(int arr[], int n) // 4 2 6 5 7

{

    int i, j, key;

    for (i = 1; i < n; i++)

    {

        key = arr[i];

        for (j = i - 1; j >= 0 && arr[j] > key; j--)

        {

            arr[j + 1] = arr[j];

        }

        arr[j + 1] = key;

    }

}

=========================================================================

3) Write a C program to create a string array with 5 elements which contains word starting

with vowel and sort them using Selection sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 void selectionSort(char s[][30], int n);

 int main()

{

    char str[5][30];

    int i, j, n = 5;

    printf("Enter the 5  words starting with vowel :\n");

 

    printf("Enter the Words : \n");

    for (i = 0; i < n; i++)

    {

        printf("index[%d] : ", i);

        scanf("%s", &str[i]);

    }

 

    selectionSort(str, n);

 

    printf("Words Array is :\n");

    for (i = 0; i < n; i++)

    {

 

        printf("%s\t", str[i]);

    }

 

    getch();

    return 0;

}

 

void selectionSort(char arr[][30], int n)

{

    char temp[30], temp2[30];

    int i, j;

 

    for (i = 0; i < n; i++)

    {

        strcpy(temp, arr[i]);

 

        for (j = i + 1; j < n; j++)

        {

            if (strcmp(temp, arr[j]) > 0)

            {

                strcpy(temp2, temp);

                strcpy(temp, arr[j]);

                strcpy(arr[j], temp2);

            }

        }

 

        strcpy(arr[i], temp);

    }

}

==========================================================================

Set A :

1) Write a C program to accept and sort n elements in ascending order by using bubble sort.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

void bubbleSort(int arr[], int n);

int main()

{

    int a[20], n, i;

    printf("Enter the array size : \n");

    scanf("%d", &n);

    printf("Enter the array elements : \n");

    for (i = 0; i < n; i++)

    {

        printf("Enter value of this index [%d] : ", i);

        scanf("%d", &a[i]);

    }

    bubbleSort(a, n); // calling Function

    printf("Array after implementing bubble sort: \n");

    for (int i = 0; i < n; i++)

    {

        printf("%d\t", a[i]);

    }

    getch();

    return 0;

}

void bubbleSort(int arr[], int n)

{

    int i, step, temp;

    for (step = 1; step < n; step++)

    {

        for (i = 0; i < n - step; i++)

        {

            if (arr[i] > arr[i + 1])

            {

                temp = arr[i];

                arr[i] = arr[i + 1];

                arr[i + 1] = temp;

            }

        }

    }

}

=================================================================

2) Write a C program to accept and sort n elements in ascending order by using insertion sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

    int a[20], i, n;

    printf("Enter the size of Array  : ");

    scanf("%d", &n);

    printf("Enter the unsorted array element : \n");

    for (i = 0; i < n; i++)

    {

        printf("Array index[%d] : ", i);

        scanf("%d", &a[i]);

    }

    insertionSort(a, n); // function calling

    printf("Sorted array element is : \n");

    for (i = 0; i < n; i++)

    {

        printf("\t %d", a[i]);

    }

 

    getch();

 

    return 0;

}

 

void insertionSort(int a[], int n) // 5 2 6 1 9

 

{

 

    int i, j, key;

    for (i = 1; i < n; i++)

    {

        key = a[i];

        for (j = i - 1; j >= 0 && key < a[j]; j--)

        {

            a[j + 1] = a[j];

        }

        a[j + 1] = key;

    }

}

====================================================================

3) Write a ‘C’ program to accept and sort n elements in ascending order using Selection sort method.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

    int a[20], n, i;

    printf("Enter the array size : \n");

    scanf("%d", &n);

    printf("Enter the array elements : \n");

    for (i = 0; i < n; i++)

    {

        printf("Enter value of this index [%d] : ", i);

        scanf("%d", &a[i]);

    }

 

    selectionSort(a, n);

 

    printf("Sorted array is : \n");

    displayArray(a, n);

 

    getch();

    return 0;

}

 

selectionSort(int arr[], int n)

{

    int i, j, temp, temp2;

 

    for (i = 0; i < n; i++)

    {

        temp = arr[i];

        for (j = i + 1; j < n; j++)

        {

            if (temp > arr[j])

            {

                temp2 = temp;

                temp = arr[j];

                arr[j] = temp2;

            }

 

            arr[i] = temp;

        }

    }

}

 

void displayArray(int arr[], int n)

{

    for (int i = 0; i < n; i++)

    {

        printf("\t%d", arr[i]);

    }

}

===================================================================

Set B :

1) Write a C program to create a string array with day of week and sort them using Insertion sort

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

    char str[][20] = {

        "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};

    int n, i;

    n = sizeof(str) / sizeof(str[0]);

 

    strInsertionSort(str, n);

    printf("Sorted array is : \n");

    for (i = 0; i < n; i++)

    {

        printf("%s \n", str[i]);

    }

 

    getch();

    return 0;

}

 

void strInsertionSort(char arr[][20], int n)

{

    int i, j;

    char key[20];

 

    for (i = 1; i < n; i++)

    {

        strcpy(key, arr[i]);

        j = i - 1;

        while (j >= 0 && strcmp(arr[j], key) > 0)

        {

            strcpy(arr[j + 1], arr[j]);

            j--;

        }

        strcpy(arr[j + 1], key);

    }

}

======================================================================

2) Write a ‘C’ program to accept names from the user and sort in alphabetical order using

bubble sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

#define Max_Len 20

 

int main()

{

    int i, n, j;

 

    printf("Enter the array size : \n");

    scanf("%d", &n);

 

    char str[n][Max_Len];

 

    printf("Enter the array elements : \n");

    for (i = 0; i < n; i++)

    {

        printf("index[%d] : ", i);

        scanf("%s", &str[i]);

    }

 

    strBubbleSort(str, n);

 

    printf("Sorted Array is : \n");

    for (i = 0; i < n; i++)

    {

        printf("%s\t", str[i]);

    }

 

    getch();

    return 0;

}

 

void strBubbleSort(char arr[][Max_Len], int n)

{

    int i, j;

    char temp[Max_Len];

 

    for (i = 1; i < n; i++)

    {

        for (j = 0; j < n - i; j++)

        {

            if (strcmp(arr[j], arr[j + 1]) > 0)

            {

                strcpy(temp, arr[j]);

                strcpy(arr[j], arr[j + 1]);

                strcpy(arr[j + 1], temp);

            }

        }

    }

}

=====================================================================

3) Write a C program to accept and sort n elements in ascending order by using bubble sort and also count the number of swaps. Display the sorted list and total no of swap count.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

int count = 0;

void bubbleSort(int arr[], int n);

int main()

{

    int a[20], n, i;

    printf("Enter the array size : \n");

    scanf("%d", &n);

    printf("Enter the array elements : \n");

    for (i = 0; i < n; i++)

    {

        printf("Enter value of this index [%d] : ", i);

        scanf("%d", &a[i]);

    }

    bubbleSort(a, n); // calling Function

    printf("\nArray after implementing bubble sort: \n");

    for (int i = 0; i < n; i++)

    {

        printf("%d\t", a[i]);

    }

 

    printf("\nTotal no of swap is : %d \n", count);

    getch();

    return 0;

}

void bubbleSort(int arr[], int n)

{

    int i, step, temp;

    for (step = 1; step < n; step++)

    {

        for (i = 0; i < n - step; i++)

        {

            if (arr[i] > arr[i + 1])

            {

                temp = arr[i];

                arr[i] = arr[i + 1];

                arr[i + 1] = temp;

                count++;

            }

        }

    }

}

===========================================================================

SET C:

1) Write a C program to read the data from the file “employee.txt” which contains empno and empname and sort the data on names alphabetically (use strcmp) using Bubble Sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

struct employee

{

    int id;

    char name[30];

};

 

int main()

{

    FILE *ptr;

    struct employee emp[10];

    int i = 0, j, size;

    char ch;

 

    ptr = fopen("employee.txt", "r");

 

    while (ch != EOF)

    {

        fscanf(ptr, "%d %s", &emp[i].id, &emp[i].name);

        ch = fgetc(ptr);

        i++;

        printf(" done \n");

    }

 

    size = i;

    bubbleSort(emp, size);

 

    ptr = fopen("employee1.txt", "w");

 

    for (i = 0; i < size; i++)

    {

        fprintf(ptr, "%d %s \n", emp[i].id, emp[i].name);

        printf("print done \n");

    }

 

    fclose(ptr);

    getch();

    return 0;

}

 

void bubbleSort(struct employee arr[], int size)

{

    struct employee temp;

    int i, j;

 

    for (i = 1; i < size; i++)

    {

        for (j = 0; j < size - i; j++)

        {

            if (strcmp(arr[j].name, arr[j + 1].name) > 0)

            {

                temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

    }

}

==========================================================================

2) Write a C program to read the data from the file “person.txt” which contains personno and  personage and sort the data on age in ascending order using insertion Sort / Selection Sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

    FILE *fptr;

    int p_no[10], p_age[10];

    int i, size;

    char ch;

    fptr = fopen("person.txt", "r");

 

    if (fptr == NULL)

    {

        printf("Error opening file \n");

        return 1;

    }

 

    i = 0;

    while (ch != EOF)

    {

        fscanf(fptr, "%d", &p_no[i]);

        fscanf(fptr, "%d", &p_age[i]);

        i++;

        ch = fgetc(fptr);

    }

 

    size = i - 1;

    insertionSort(p_age, size);

 

    fptr = fopen("p.txt", "w");

 

    if (fptr == NULL)

    {

        printf("Error file cannot open\n");

    }

 

    for (i = 0; i <= size; i++)

    {

        fprintf(fptr, "%d %d\n", p_no[i], p_age[i]);

        printf("done\n");

    }

 

    // for (int j = 0; j < size; j++)

    // {

    //     printf("%d\t", p_age[j]);

    // }

 

    fclose(fptr);

    getch();

    return 0;

}

 

void insertionSort(int arr[], int size)

{

    int i, j, key;

 

    for (i = 1; i < size; i++)

    {

        key = arr[i];

 

        for (j = i - 1; j >= 0 && arr[j] > key; j--)

        {

            arr[j + 1] = arr[j];

        }

 

        arr[j + 1] = key;

    }

}

==========================================================================

3) Modify the bubble sort, insertion sort and selection sort program of Set A to sort the integers in descending order?

 

Post a Comment

0 Comments

Ad Code

Responsive Advertisement