Professional Documents
Culture Documents
Hamid (Lab 13)
Hamid (Lab 13)
Hamid (Lab 13)
Topic: Sorting
Lab Tasks
1. Read 10 random numbers from the user into an array. Sort the data in ascending order
using the simplest form of bubble sort algorithm that you learned during the previous
lecture. Present the sorted data on the screen.
2. The bubble sort algorithm is inefficient for large arrays. Make the following simple
modifications to improve the performance of the bubble sort.
b) The data in the array may already be in the proper order or near-proper
order, so why make nine passes if fewer will suffice? Modify the sort to
check at the end of each pass if any swaps have been made. If none has
been made, then the data must already be in the proper order, so the
program should terminate. If swaps have been made, then at least one
more pass is needed.
Your program output should look like the following sample output:
4. In statistics mean, median, and mode are three very important terminologies that are used
to analyze the results of surveys and opinion polls. Consider the following set of data :
2, 10, 5, 6, 2, 8, 4, 2, 10, 5, 8
The mean is the arithmetic average of the given values, where the values are summed
first and then divided by the total number of values. The mean for the example data
would be sum (2, 10, 5, 6, 2, 8, 4, 2, 10, 5, 8) / 11 which is 6.
The median is the middle value after the given values have been sorted into an ascending
order. When the number of elements is even, the median should be calculated as the
mean of the two middle elements.The medianfor the exmaple data is 5 which is the
middle element of the sorted data 2,2,2,4,5,5,6,8,8,10,10
The mode is the value that occurs most frequently among all the given values. The mode
for the example data is 2 which is the most frequent value with a frequency of 3.
int sum,i;
sort(arr);
for(i=0;i<SIZE;i++)
{
sum = sum + arr[i];
printf("%d",arr[i]);
}
return sum/SIZE;
int sum,i,frequency[10]={0},freq,value=0;
sort(arr);//sorted array
for(i=0;i<SIZE;i++)
{
++frequency[arr[i]];
}
freq = frequency[0];
for(i=1;i<=9;i++)//Numbers are from 0 to 9
{
if(freq<frequency[i])//comparison of occurrence
{
freq = frequency[i];
value = i;
}
}
printf("mod occurs at %d times\n",freq);
return value;
e) Now, Using the above defined functions, write a program that generates a 99-
element array of random numbers between 1 and 100 (inclusive) and prints on
screen its mean, median, and the mode.
Task 2
Code:
//Hamid Muzaffar Khan
//CMS ID : 356209
#include<stdio.h>
#define SIZE 10
int main()
{
int i, arr[SIZE], j, hold, count;
for (i = 0; i<SIZE; i++)
{
scanf_s("%d", &arr[i]);//Take the array elements
}
for (i = 0; i<SIZE - 1; i++)
{
int swap = 0;//Count no. of swapping
for (j = 0; j < SIZE - i - 1; j++)
{
if (arr[j]>arr[j + 1])
{
hold = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = hold;
swap++;//Swap no. 1
Output:
“Task 3”
Output:
Output: