Ad Code

Responsive Advertisement

Data structure assignment 03 : Sorting Techniques (Recursive)


 download source code file click

Practice Programs:

1) Write a C program to create a integer array with elements {888,111,666,444,222,999,333} and sort the given array using Merge sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

    int arr[] = {888, 111, 666, 444, 222, 999, 333};

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

    mergeSort(arr, 0, size - 1);

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

    {

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

    }

    getch();

    return 0;

}

void mergeSort(int arr[], int low, int high)

{

    int mid;

 

    if (low < high)

    {

        mid = (low + high) / 2;

        mergeSort(arr, low, mid);

        mergeSort(arr, mid + 1, high);

        merge(arr, low, mid, high);

    }

}

void merge(int arr[], int low, int mid, int high)

{

    int i, j, k;

    int b[20];

    i = low;

    j = mid + 1;

    k = 0;

 

    while (i <= mid && j <= high)

    {

        if (arr[i] < arr[j])

        {

            b[k++] = arr[i++];

        }

        else

        {

            b[k++] = arr[j++];

        }

    }

    while (i <= mid)

    {

        b[k++] = arr[i++];

    }

    while (j <= high)

    {

        b[k++] = arr[j++];

    }

    for (j = low, k = 0; j <= high; k++, j++)

    {

        arr[j] = b[k];

    }

}

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

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

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

void quickSort(int arr[], int low, int high);

int partitionArray(int arr[], int low, int high);

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]);

    }

 

    quickSort(a, 0, n - 1);

 

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

    {

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

    }

    getch();

    return 0;

}

 

void quickSort(int arr[], int low, int high)

{

    int j;

    if (low < high)

    {

        j = partitionArray(arr, low, high);

        quickSort(arr, low, j - 1);

        quickSort(arr, j + 1, high);

    }

}

 

int partitionArray(int arr[], int low, int high) // 5 4 3 2 1

{

    int i, j;

    int temp, pivot;

    pivot = arr[low];

    i = low + 1;

    j = high;

 

    do

    {

 

        while (pivot > arr[i] && i <= high)

        {

            i++;

        }

 

        while (pivot < arr[j] && j >= low)

        {

            j--;

        }

 

        if (i < j)

        {

            temp = arr[i];

            arr[i] = arr[j];

            arr[j] = temp;

        }

    } while (i < j);

 

    arr[low] = arr[j];

    arr[j] = pivot;

 

    return j;

}

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

3) Write a C program to sort a random array of n integers (value of n accepted from user) by using merge Sort algorithm in ascending order

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

    int arr[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", &arr[i]);

    }

 

    mergeSort(arr, 0, n - 1);

 

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

 

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

 

    {

 

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

    }

    getch();

    return 0;

}

 

void mergeSort(int arr[], int low, int high)

{

    int mid;

    if (low < high)

    {

        mid = (low + high) / 2;

        mergeSort(arr, low, mid);

        mergeSort(arr, mid + 1, high);

        merge(arr, low, mid, high);

    }

}

 

void merge(int arr[], int low, int mid, int high)

{

    int i, j, k;

    int b[20];

    i = low;

    j = mid + 1;

    k = 0;

 

    while (i <= mid && j <= high)

    {

        if (arr[i] < arr[j])

        {

            b[k++] = arr[i++];

        }

        else

        {

            b[k++] = arr[j++];

        }

    }

 

    while (i <= mid)

    {

        b[k++] = arr[i++];

    }

 

    while (j <= high)

    {

        b[k++] = arr[j++];

    }

 

    for (j = low, k = 0; j <= high; k++, j++)

    {

        arr[j] = b[k];

    }

}

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

SET A:

1) Write a C program to accept and sort n elements in ascending order by using merge 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  array element : \n");

 

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

    {

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

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

    }

 

    mergeSort(a, 0, n - 1);

 

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

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

    {

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

    }

    getch();

    return 0;

}

 

void mergeSort(int arr[], int low, int high)

{

    int mid;

    if (low < high)

    {

        mid = (low + high) / 2;

        mergeSort(arr, low, mid);

        mergeSort(arr, mid + 1, high);

        merge(arr, low, mid, high);

    }

}

 

void merge(int arr[], int low, int mid, int high)

{

    int i, j, k, arr2[20];

    i = low;

    j = mid + 1;

    k = 0;

 

    while (i <= mid && j <= high)

    {

        if (arr[i] < arr[j])

        {

            arr2[k++] = arr[i++];

        }

        else

        {

            arr2[k++] = arr[j++];

        }

    }

 

    while (i <= mid)

    {

        arr2[k++] = arr[i++];

    }

 

    while (j <= high)

    {

        arr2[k++] = arr[j++];

    }

 

    for (i = low, k = 0; i <= high; i++, k++)

    {

        arr[i] = arr2[k];

    }

}

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

2) Write a C program to accept and sort n elements in ascending order by using quick 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  array element : \n");

 

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

    {

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

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

    }

 

    quickSort(a, 0, n - 1);

 

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

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

    {

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

    }

    getch();

    return 0;

}

 

void quickSort(int arr[], int low, int high)

{

    int j;

 

    if (low < high)

    {

        j = partitionArray(arr, low, high);

        quickSort(arr, low, j - 1);

        quickSort(arr, j + 1, high);

    }

}

 

int partitionArray(int arr[], int low, int high)

{

    int pivot, temp;

    int i, j;

 

    i = low + 1;

    j = high;

    pivot = arr[low];

 

    do

    {

        while (arr[i] < pivot && i <= high)

        {

            i++;

        }

 

        while (arr[j] > pivot && j >= low)

        {

            j--;

        }

 

        if (i < j)

        {

            temp = arr[i];

            arr[i] = arr[j];

            arr[j] = temp;

        }

    } while (i < j);

 

    arr[low] = arr[j];

    arr[j] = pivot;

 

    return j;

}

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

3) Modify the Quick sort program of SET A to sort the integers in descending order?

 

#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  array element : \n");

 

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

    {

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

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

    }

 

    mergeSort(a, 0, n - 1);

 

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

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

    {

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

    }

    getch();

    return 0;

}

 

void mergeSort(int arr[], int low, int high)

{

    int mid;

    if (low < high)

    {

        mid = (low + high) / 2;

        mergeSort(arr, low, mid);

        mergeSort(arr, mid + 1, high);

        merge(arr, low, mid, high);

    }

}

 

void merge(int arr[], int low, int mid, int high)

{

    int i, j, k, arr2[20];

    i = low;

    j = mid + 1;

    k = 0;

 

    while (i <= mid && j <= high)

    {

        if (arr[i] > arr[j])

        {

            arr2[k++] = arr[i++];

        }

        else

        {

            arr2[k++] = arr[j++];

        }

    }

 

    while (i <= mid)

    {

        arr2[k++] = arr[i++];

    }

 

    while (j <= high)

    {

        arr2[k++] = arr[j++];

    }

 

    for (i = low, k = 0; i <= high; i++, k++)

    {

        arr[i] = arr2[k];

    }

}

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

SET B:

1) Write a C program to create a string array with months (accept atleast 6 month) and sort them using Quick sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#define MaxLen 20

void strQuickSort(char str[][MaxLen], int low, int high);

int partitionStrArray(char str[][MaxLen], int low, int high);

 

int main()

{

    int i, n;

 

    printf("Enter the min 6 months or more : \n");

    scanf("%d", &n);

 

    char str[n][MaxLen]; // string Array

 

    printf("Enter the months name : \n");

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

    {

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

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

    }

 

    strQuickSort(str, 0, n - 1); // function call

 

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

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

    {

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

    }

 

    getch();

    return 0;

}

 

void strQuickSort(char str[][MaxLen], int low, int high)

{

 

    int j;

 

    if (low < high)

    {

        j = partitionStrArray(str, low, high);

        strQuickSort(str, low, j - 1);

        strQuickSort(str, j + 1, high);

        printf("if called \n");

    }

    printf("main called \n");

}

 

int partitionStrArray(char str[][MaxLen], int low, int high)

{

    int i, j;

    char pivot[MaxLen], temp[MaxLen];

    i = low + 1;

    j = high;

    strcpy(pivot, str[low]);

 

    do

    {

        while (strcmp(str[i], pivot) == -1 && i <= high)

        {

            i++;

        }

 

        while (strcmp(str[j], pivot) == 1 && j >= low)

        {

            j--;

        }

 

        if (i < j)

        {

            strcpy(temp, str[i]);

            strcpy(str[i], str[j]);

            strcpy(str[j], temp);

        }

 

    } while (i < j);

 

    strcpy(str[low], str[j]);

    strcpy(str[j], pivot);

 

    return j;

}

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

2) Write a C program to create a string array with at least 5 elements which contains word

ending with ‘at’ and ‘an’ sound and sort them using Merge sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#define MaxLen 20

 

void strMergeSort(char str[][MaxLen], int low, int high);

void mergeStr(char str[][MaxLen], int low, int mid, int high);

 

int n; // Global variable

 

int main()

{

    int i;

 

    printf("Enter the at least 5 words ending with at and an : \n");

    scanf("%d", &n);

 

    char str[n][MaxLen]; // string Array

 

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

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

    {

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

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

    }

 

    strMergeSort(str, 0, n - 1); // function call

 

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

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

    {

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

    }

 

    getch();

    return 0;

}

 

void strMergeSort(char str[][MaxLen], int low, int high)

{

    int mid;

 

    if (low < high)

    {

        mid = (low + high) / 2;

        strMergeSort(str, low, mid);

        strMergeSort(str, mid + 1, high);

        mergeStr(str, low, mid, high);

    }

}

 

void mergeStr(char str[][MaxLen], int low, int mid, int high)

{

    char cpy[n][MaxLen];

    int i, j, k;

    i = low;

    j = mid + 1;

    k = 0;

 

    while (i <= mid && j <= high)

    {

        if (strcmp(str[i], str[j]) > 0)

        {

            strcpy(cpy[k++], str[j++]);

        }

        else

        {

            strcpy(cpy[k++], str[i++]);

        }

    }

 

    while (i <= mid)

    {

        strcpy(cpy[k++], str[i++]);

    }

 

    while (j <= high)

    {

        strcpy(cpy[k++], str[j++]);

    }

 

    for (i = low, k = 0; i <= high; k++, i++)

    {

        strcpy(str[i], cpy[k]);

    }

}

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

3) Modify the Merge sort program of Set B to sort the integers in descending order?

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

 

// function declaration

 

void merge(int arr[], int firstIndex, int midIndex, int lastIndex);

void mergeSort(int arr[], int firstIndex, int lastIndex);

 

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]);

    }

 

    mergeSort(a, 0, n - 1);

 

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

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

    {

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

    }

 

    getch();

 

    return 0;

}

 

// Function definition

void merge(int arr[], int firstIndex, int midIndex, int lastIndex)

{

 

    int i, j, k, b[20];

    i = firstIndex;

    j = midIndex + 1;

    k = 0;

    while (i <= midIndex && j <= lastIndex)

 

    {

 

        if (arr[i] < arr[j])

        {

            b[k++] = arr[j++];

        }

        else

 

        {

            b[k++] = arr[i++];

        }

    }

    while (i <= midIndex)

 

    {

        b[k++] = arr[i++];

    }

 

    while (j <= lastIndex)

    {

        b[k++] = arr[j++];

    }

 

    // copy the b array into original array (arr[])

    for (j = firstIndex, k = 0; j <= lastIndex; j++, k++)

    {

        arr[j] = b[k];

    }

}

 

void mergeSort(int arr[], int firstIndex, int lastIndex)

{

    int mid;

 

    if (firstIndex < lastIndex)

    {

 

        mid = (firstIndex + lastIndex) / 2;

        mergeSort(arr, firstIndex, mid);

        mergeSort(arr, mid + 1, lastIndex);

        merge(arr, firstIndex, mid, lastIndex);

    }

}

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

SET C:

1) Write a C program to read the data from the file “person.txt” which contains personno,

name and personage and sort the data on age in ascending order using merge Sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#define Max_Len 20

 

struct person

{

    int per_no;

    char name[Max_Len];

    int age;

};

 

void mergeSort(struct person arr[], int low, int high);

void merge(struct person arr[], int low, int mid, int high);

 

int main()

{

    FILE *fptr;

    int i, size;

    struct person p1[Max_Len];

    fptr = fopen("01_person.txt", "r");

 

    if (fptr == NULL)

    {

        printf("File not Found !");

        return 0;

    }

 

    for (i = 0; !feof(fptr); i++)

    {

        fscanf(fptr, "%d %s %d", &p1[i].per_no, &p1[i].name, &p1[i].age);

        printf("%d %s %d \n", p1[i].per_no, p1[i].name, p1[i].age);

    }

 

    size = i;

 

    mergeSort(p1, 0, size - 1);

 

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

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

    {

        printf("%d %s %d \n", p1[i].per_no, p1[i].name, p1[i].age);

    }

 

    fptr = fopen("01_person.txt", "a");

    fprintf(fptr, "\n Sorted Records is :\n");

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

    {

        fprintf(fptr, "%d %s %d \n", p1[j].per_no, p1[j].name, p1[j].age);

    }

 

    fclose(fptr);

    getch();

    return 0;

}

 

void mergeSort(struct person arr[], int low, int high)

{

    int mid;

    if (low < high)

    {

        mid = (low + high) / 2;

        mergeSort(arr, low, mid);

        mergeSort(arr, mid + 1, high);

        merge(arr, low, mid, high);

    }

}

 

void merge(struct person arr[], int low, int mid, int high)

{

    int i, j, k;

    struct person cpy[Max_Len];

    i = low;

    j = mid + 1;

    k = 0;

 

    while (i <= mid && j <= high)

    {

        if (arr[i].age < arr[j].age)

        {

            cpy[k++] = arr[i++];

        }

        else

        {

            cpy[k++] = arr[j++];

        }

    }

 

    while (i <= mid)

    {

        cpy[k++] = arr[i++];

    }

    while (j <= high)

    {

        cpy[k++] = arr[j++];

    }

 

    for (i = low, k = 0; i <= high; k++, i++)

    {

        arr[i] = cpy[k];

    }

}

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

2) Write a C program to read the data from the file “student.txt” which contains rollno, name and age and sort the data on age in ascending order using quick Sort.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#define Max_Len 20

 

struct student

{

    int roll_no;

    char name[Max_Len];

    int age;

};

 

// Function Declaration

void quickSort(struct student arr[], int low, int high);

int partitionArray(struct student arr[], int low, int high);

 

int main()

{

    FILE *fp;

    int i, size;

    struct student s1[Max_Len];

 

    fp = fopen("02_student.txt", "r");

 

    if (fp == NULL)

    {

        printf("File does not exist\n");

        return 1;

    }

 

    for (i = 0; !feof(fp); i++)

    {

        fscanf(fp, "%d %s %d", &s1[i].roll_no, &s1[i].name, &s1[i].age);

        printf("%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);

    }

 

    size = i;

 

    quickSort(s1, 0, size - 1);

 

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

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

    {

        printf("%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);

    }

 

    fp = fopen("02_student.txt", "a");

    fprintf(fp, "\n Sorted Elements is : \n");

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

    {

        fprintf(fp, "%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);

    }

 

    fclose(fp);

    getch();

    return 0;

}

 

void quickSort(struct student arr[], int low, int high)

{

    int j;

 

    if (low < high)

    {

        j = partitionArray(arr, low, high);

        quickSort(arr, low, j - 1);

        quickSort(arr, j + 1, high);

    }

}

 

int partitionArray(struct student arr[], int low, int high)

{

    int i, j;

    struct student pivot, temp;

 

    pivot = arr[low];

    i = low + 1;

    j = high;

 

    do

    {

 

        while (arr[i].age < pivot.age && i <= high)

        {

            i++;

        }

 

        while (arr[j].age > pivot.age && j >= low)

        {

            j--;

        }

 

        if (i < j)

        {

            temp = arr[j];

            arr[j] = arr[i];

            arr[i] = temp;

        }

 

    } while (i < j);

 

    arr[low] = arr[j];

    arr[j] = pivot;

    return j;

}

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

3) Read the data from the file student.txt and sort on names in alphabetical order (use strcmp) using Merge sort / Quick sort. Write the sorted data to another file 'sortstudentname.txt'.

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#define Max_Len 20

 

struct student

{

    int roll_no;

    char name[Max_Len];

    int age;

};

 

void mergeSort(struct student arr[], int low, int high);

void merge(struct student arr[], int low, int mid, int high);

 

int main()

{

    FILE *fptr, *fp;

    int i, size;

    struct student s1[Max_Len];

 

    fptr = fopen("03_student.txt", "r");

 

    for (i = 0; !feof(fptr); i++)

    {

        fscanf(fptr, "%d %s %d", &s1[i].roll_no, &s1[i].name, &s1[i].age);

        printf("%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);

    }

 

    size = i;

 

    mergeSort(s1, 0, size - 1);

 

    fp = fopen("sort_student_name.txt", "w");

 

    fprintf(fp, "Sorted Student Records is : \n");

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

    {

        fprintf(fp, "%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);

    }

 

    printf("\nSorted Array element is : \n");

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

    {

        printf("%d %s %d\n", s1[j].roll_no, s1[j].name, s1[j].age);

    }

 

    fclose(fptr);

    fclose(fp);

    getch();

    return 0;

}

 

void mergeSort(struct student arr[], int low, int high)

{

    int mid;

 

    if (low < high)

    {

        mid = (low + high) / 2;

        mergeSort(arr, low, mid);

        mergeSort(arr, mid + 1, high);

        merge(arr, low, mid, high);

    }

}

 

void merge(struct student arr[], int low, int mid, int high)

{

    int i, j, k;

    struct student copy[Max_Len];

    i = low;

    j = mid + 1;

    k = 0;

 

    while (i <= mid && j <= high)

    {

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

        {

            copy[k++] = arr[j++];

        }

        else

        {

            copy[k++] = arr[i++];

        }

    }

 

    while (i <= mid)

    {

        copy[k++] = arr[i++];

    }

    while (j <= high)

    {

        copy[k++] = arr[j++];

    }

 

    for (i = low, k = 0; i <= high; i++, k++)

    {

        arr[i] = copy[k];

    }

}

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

 

Post a Comment

0 Comments

Ad Code

Responsive Advertisement