Sie sind auf Seite 1von 7

RAGHU INSTITUTE OF TECHNOLOGY

RAGHU INSTITUTE OF TECHNOLOGY


DEPARTMENT OF CSE
College Code: 3J

R13 Syllabus

C PROGRAMMING LAB MANUAL


===================================================================
Exercise 6
a) Write a C program to implement sorting of an array of elements .
b) Write a C program to input two m x n matrices, check the compatibility and perform addition
and multiplication of them.
===================================================================
SPECIFICATION:
(6)(a) Write a c-program to implement sorting of an array of elements.
DESCRIPTION:
First we have to take an array of elements in an unsorted fashion and make them sorted using one
of the sorting techniques. As the term sorting refers to the bringing some orderliness in the data
which are not ordered.
Many sorting methods are advocated. Each method has its own advantages and disadvantages.
Any sorting method takes two elements at a time. It compares these two elements. If the first
element is less than the second element, they are left undisturbed. If the first element is greater
then the second element, then they are swapped. And the same procedure continues with the next
two elements and goes on until all the elements in the array are sorted. This sorting method is
explained with the following example. Consider a list 74,39,35,97 and 84.
Pass1:
In pass1 the first element is compared with all the other elements. And the first element may
change during the process.
1. Compare 74 and 39. Since 74 > 39, they are swapped. The array now changes like
39,74,35,97 and 84.
2. Compare 39 and 35 since the first element is 39. As 39 > 35, they are swapped. The array
now changes like 35,39,74,97 and 84.
3. Compare 35 and 97 since 35 is the first element and 39, 74 are already compared. As 35
< 97, they are undisturbed. The array is now 35,39,74,97 and 84.
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

4. Now compare 35 and 84, since 35 < 84 they are undisturbed. The array is now
35,39,74,97 and 84.
At the end of this pass, the first element will be in correct place. The second pass begins with the
second element and is compared with all other elements. And the number of comparisons will be
reduced by one since the first element is not compared any more.
Pass 2:
Here the second element will be compared.
1. Compare 39 and 74. Since 39 < 74, they are undisturbed. The array is now 35,39,74,97
and 84.
2. Compare 39 and 97. Since 39 < 97, they are undisturbed. The array is now 35,39,74,97
and 84.
3. Compare 39 and 84. Since 39 < 84, they are undisturbed. The array is now 35,39,74,97
and 84.
This pass does not bring any change in the array elements. The next pass starts.
Pass 3:
Here the third element is compared.
1. Compare 74 and 97. Since 74< 97, they are undisturbed. The array is now 35,39,74,97
and 84.
2. Compare 74 and 84. Since 74< 84, they are undisturbed. The array is now 35,39,74,97
and 84
This pass also does not bring any change in the array elements. The next pass starts.
Pass 4:
Here the fourth element is compared.
1. Compare 97and 84. Since 97>84, they are swapped. The array is now 35,39,74,84 and 97.
The sorting ends here. And the last number is not compared with any other number.
ALGORITHM:
STEP-1: Start.
STEP-2: Declare integer variables i,j,temp,size,a[50].
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

STEP-3: Read integer variables size,a[i].


STEP-4: Check the conditions and the iterations.
4.1: First iteration is checked where I is initialized as 0 and (i<size) is checked and i is
incremented until the loop fails.
4.2: Inner loop is executed where j is initialized as i+1 and (j<size) is checked and j is
incremented until the loop fails.
4.3: If condition (a[i]>a[j]) is checked if true:
temp

a[i]

a[i]

a[j]

a[j]

temp

4.3.1: if condition is false comes out of the if condition and also the loop.
STEP-5: Print the elements.
STEP-6: Stop.
FLOWCHART:

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

START

DECLARE a[50],i,j,temp,size
READ size
For i in steps of 1 do where i<size
FALSE
TRUE

READ a[i]
FALSE
For i in steps of 1 do where i<size
TRUE
For j in steps of 1 do where j<size
FALSE
TRUE
FALSE

(a[i]>a[j])
TRUE
temp=a[i]
a[i]=a[j]
a[j]=temp

STOP

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program to Sorting of array of elements */
Program name:
/* Done By : C-Faculty
#include<stdio.h>
#include<curses.h>
int main()
{
int a[50],i,j,temp,size;
clear();
printf(enter the size of an array);
scanf(%d,&size);
printf(enter the elements);
for(i=0;i<size;i++)
{
scanf(%d,&a[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
temp=a[i];
Department of Computer Science & Engg

// wk6a.c
Dated: 15/10/2013*/

RAGHU INSTITUTE OF TECHNOLOGY

a[i]=a[j];
a[j]=temp;
}
}
printf(sorted elements are: );
for(i=0;i<size;i++)
{
printf(%d,size);
}
}
return(0);
}
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the
program and quit)
Step 2: Now compile the program by using the following command
cc wk6a.c lcurses -lm
Step 3: Now go for running the program by using the command
./a.out

Step 4: To create an executing file use the command


cc wk6a.c -curses o sorting

EXPECTED I/P AND O/P:


OUTPUT-1: enter the size 4
Enter the elements 4 2 6 1
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Sorted elements are 1 2 4 6


OUTPUT-2: Enter the size 3
Enter the elements 7 3 9
Sorted elements are 3 7 9
ORIGINAL OUTPUT :
OUTPUT-1: enter the size 4
Enter the elements 4 2 6 1
Sorted elements are 1 2 4 6
OUTPUT-2: Enter the size 3
Enter the elements 7 3 9
Sorted elements are 3 7 9
VIVA VOCE QUESTIONS:
1. What is meant by sorting?
The term sorting means bringing some orderliness in the data, which is not

ordered.

2. Define array?
An array is a group of related items that share a common name.
3. Mention few sorting techniques used for sorting elements?
Bubble sort, insertion sort, merge sort, quick sort are some of the sorting techniques that are
used to sort the elements.
4. What is the minimum condition required for sorting the elements?
The first element is compared with the next element. If the first element is greater than the
second one then they should be swapped otherwise they remain unchanged.
5. On what factors the efficiency of the sorting is measured?
The efficiency is measured in terms of memory usage, time taken to sort, CPU usage.
--xXx-Department of Computer Science & Engg

Das könnte Ihnen auch gefallen