Sunday, August 20, 2023

SYBSc(CS) Sem III : Data Structure Slip 6 : Que - 1

 

SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

SUBJECT: CS-233 Practical course based on CS231

/* Q.1 Sort a random array of n integers (accept the value of n from user) in ascending order by using selection sort algorithm */

#include<stdio.h>

#include<stdlib.h>

#define MAX 100

// CheckFor:  Create new Array 

void CreateArray(int A[], int n){

     

     // CheckFor: Declaration of local variable

     int i = 0;

        

     // LoopFor: Assingn the random array elements

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

          A[i] = rand(); 

}


// CheckFor:  Display the Array

void DisplayArray(int A[], int n){


     // CheckFor: Declaration of local variable     

     int i = 0;

     

     // LoopFor: Display the array elements

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

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

}


// CheckFor:  Sort the Array Element using Selection Sort Algorithm

void SelectionSort(int A[], int n){

     

     // CheckFor: Declaration of local variable

     int i = 0, j = 0, loc = 0, min = 0, temp = 0;

     

     // LoopFor: Moving the Array from first to last element

     for ( i = 0; i < n-1; i++){

         

         // CheckFor: Assign the minimum element of Array and location

         min = A[i];

         loc = i;

         

         // LoopFor: Select the next minimum element from Array

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

             // CheckFor: Minimum element and change it and save its location

             if ( min > A[j]){

                  min = A[j];

                  loc = j;

             }

         }

         

         // CheckFor: Swap the the minimum element its correct position

         temp = A[i];

         

         A[i] = A[loc];

         

         A[loc] = temp;

     }

     

     // CheckFor: Display the Sorted Array 

     printf("\n\nThe Sorted Array after Selection Sort is: \n\n");

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

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

}


int main(){

    int A[MAX], num = 0;

    

    // Accept the n number from user

    printf("Enter How many element we want to in array: ");

    scanf("%d", &num);

    

    // create n random element array

    CreateArray(A, num);

    

    // Display the given array

    printf("\n\nThe given array elements are:  \n\n");

    DisplayArray(A, num);

    

    // Sort the element using insertion sort algorithm

    SelectionSort(A, num);

    

    printf("\n\n");

    system("pause");

    return 0;

}


No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...