Ad Code

Responsive Advertisement

Data Structure Assignment 2 | Practice Question

Assignment 2 

Practice Programs: 

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.

answer => 


#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.

answer => 

Insertion Sort Method

#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.

answer => 

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

    }

}

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

Post a Comment

0 Comments

Ad Code

Responsive Advertisement