0% found this document useful (0 votes)
18 views

LAB-1 Assignment

The document contains code to sort an integer array using selection sort and count the number of passes and swaps during the sorting process. It then provides a second code sample that finds the second largest and second smallest numbers in an integer array by iterating through the array, tracking the largest and second largest and smallest and second smallest values. The code samples are intended as exercises or examples for a computer science course on algorithms and data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

LAB-1 Assignment

The document contains code to sort an integer array using selection sort and count the number of passes and swaps during the sorting process. It then provides a second code sample that finds the second largest and second smallest numbers in an integer array by iterating through the array, tracking the largest and second largest and smallest and second smallest values. The code samples are intended as exercises or examples for a computer science course on algorithms and data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MD Sifatullah sheikh

2022-1-60-029
Cse207 (MMLI)
Section = 8

Exercise 1:
Modify the above code to count the number
of pass and swap operation for an array of
size N.

ANS :

#include <stdio.h>
int main(){
int a[100];
int n,i,pos=-1,item,passes = 0,swaps = 0;
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter your data : ");
for(i = 0; i < n; i++){
scanf("%d",&a[i]);
}

printf("Your array :");


for(i = 0; i < n; i++){
printf("%d\t",a[i]);
}
int temp,j;
for(i = 0; i < n; i++){
for(j = 0; j < n-1-i; j++){
if(a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
swaps++;
}
}
passes++;
}

printf("\nPasses : %d and swaps :


%d",passes,swaps);
printf("\nYour array :");
for(i = 0; i < n; i++){
printf("%d\t",a[i]);
}
return 0;

}
OUTPUT :

Exercise 2
Write a program to find second maximum
and minimum number of an integer array. Show the
sample input and output clearly.
#include<stdio.h>
int main(){
int
num,i,largest,second_largest,smallest,second_smalles
t;
printf("How many elements you want :");
scanf("%d",&num);
if(num<2){
printf("Warning ! The array should have atleast
2 elements .");
return;

}
int arr[num];
printf("Enter your data :");
for( i=0 ;i<num;i++){
scanf("%d",&arr[i]);
}

largest = arr[0];
second_largest=0;

for( i=0 ;i<num;i++){


if(arr[i]>largest){
second_largest= largest;
largest=arr[i];
}
else if((arr[i]> second_largest) && (arr[i] !=
largest)){
second_largest = arr[i];
}
}

if (arr[0] < arr[1]) {


smallest = arr[0];
second_smallest = arr[1];
}
else {
smallest = arr[1];
second_smallest = arr[0];
}
for (i = 2; i < num; i++) {
if (arr[i] < smallest) {
second_smallest = smallest;
smallest = arr[i];
}
else if (arr[i] < second_smallest) {
second_smallest = arr[i];
}
}
printf(" \nSecond smallest element is %d",
second_smallest);

printf(" \nSecond largest element is %d",


second_largest);

return 0;
}
Output :

You might also like