Raghu Institute of Technology: Department of Computer Science & Engg
Raghu Institute of Technology: Department of Computer Science & Engg
R13 Syllabus
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
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:
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
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*/
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
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