Professional Documents
Culture Documents
Unit 3 GVK
Unit 3 GVK
Unit 3 GVK
Engineering &Technology
Department of ECE
SUBJECT: C PROGRAMMING
Subject Code:
G Vijaya Kumar
Assistant Professor
Email: vijayakumar_g@vnrvjiet.in
1. Linear search
2. Binary search
it is best to use when elements are arranged in order (ascending, descending etc..)
➢it is a very simple method used for searching an array for a particular value.
➢It works by comparing the value to be searched with every element of the
array one by one in a sequence until a match is found.
Step 4: If k doesn’t match any element until end, then return -1.
END
Result=index
ENDIF
ENDLOOP
END
February 10, 2022 Department of ECE, VNRVJIET, Hyderabad 7
Program : Linear Search
#include <stdio.h> for (i = 0; i < n; i++)
void main() if (Data[i] == key)
{ result = i;
int Data[5], key,i,result=-1,n; (result == -1) ? printf(">>>Element is
//int Data[] = { 2, 6, 4,8,1}; not present in array<<<")
printf("Enter elements into array\n"); : printf("Element is present at
for(i = 0; i < 5; i++) index %d", result);
{
scanf("%d",&Data[i]); }
}
n = sizeof(Data) / sizeof(Data[0]);
printf("Enter element to be searched ?\n");
scanf("%d",&key);
#define size 10 {
scanf("%d", &arr[i]);
void main()
}
{
printf("\n\n Enter search key\n");
int arr[size],key, i, n, l, u, mid, found=0;
scanf("%d", &key);
printf("\n Enter no of elements ");
l = 0, u = n-1;
scanf("%d", &n);
printf("\n Enter the elements: in
ascending order only\n");
{ u = mid-1;
else
mid = (l + u)/2;
l = mid+1;
if (arr[mid] == key)
}
{
if (l > u && found == 0)
printf("\n %d is present at %d index",
printf("\n %d does not exist in the
key, mid); array",key);
found =1; }
break;
}
2. Insertion sort
3. Selection sort
➢Sorts by repeatedly moving the largest element to the highest index position of the array(in case of
ascending order)
➢In bubble sorting, consecutive adjacent pairs of elements in the array are compared with each other.
➢If the element at the lower index is greater than the element at the higher index
➢This process will continue till the list of unsorted elements exhausts
➢This procedure of sorting is called bubble sorting because elements ‘bubble’ to the top of the list
➢If the elements are to be sorted in descending order, then in first pass
the smallest element is moved to the highest index of the array.
Example:
A[ ] ={3,2,1,4};
A[ ] ={2,3,1,4}
A[ ] ={2,1,3,4}
A[ ] ={1,2,3,4}
Note: Here end of array is reached, and next highest element is placed at
its proper position
Pass 3:
ii) compare 1 & 2 1<2 No Swapping
A[ ] ={1,2,3,4}
Pass-1
Pass-4
for(j=0;j<n-i-1;j++) for(i=0;i<n;i++)
printf("%d\t", arr[i]);
{
}
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}}
40 10 70 30 20
➢ Divide into two lists
sorted list Unsorted list
10 40 70 30 20
10 30 40 70 20
#include <conio.h> {
scanf("%d", &arr[i]);
#define size 5
}
void main(){
for(i=1;i<n;i++)
int arr[size], i,j,temp, n;
{
printf("\n Enter no of elements ");
temp = arr[i];
scanf("%d", &n); j = i-1;
printf("\n Enter the elements of the
array: ");
• preprocessors directives tell the compiler to preprocess the source code before compiling. All of these preprocessor
directives begin with a ‘#’ (hash) symbol. The ‘#’ symbol indicates that, whatever statement starts with #, is going to
the preprocessor program, and preprocessor program will execute this statement
(i) Header File or Standard files: These files contains definition of pre-defined functions
like printf(), scanf() etc. Syntax: #include <filename>
where file_name is the name of file to be included. The ‘<‘ and ‘>’ brackets tells the
compiler to look for the file in standard directory.
(ii) user defined files: When a program becomes very large, it is good practice to divide it
into smaller files and include whenever needed. These types of files are user defined files.
Syntax: #include “filename”
February 10, 2022 Department of ECE, VNRVJIET, Hyderabad 44
The # inlcude<filename> makes the preprocessor to include header files from the
system library.
The # inlcude"filename" makes the preprocessor to include files from the user-
defined directory.
#include <stdio.h>
contents of hello.c
#include <conio.h>
int display();
#include "hello.c“
int display()
main ( )
{
{
printf(“ welcome to vnrvjiet");
clrscr();
return 0;
printf (“hai”);
}
display();
}
Output
hai welcome to vnrvjiet
February 10, 2022 Department of ECE, VNRVJIET, Hyderabad 45
Macro substitution directives/commands
• Macro substitution is a process where an identifier in a program is replaced by a predefined string. The
preprocessor accomplishes this task under the direction of #define statement.
• Syntax for defining a macro # define identifier string
• If this statement is included in the program at the beginning, then the preprocessor replaces every
occurrence of the identifier in the source code by the string. The string may be any text, while the identifier
must be a valid C name.
• Note: There is no semi-colon(‘;’) at the end of macro definition. Macro definitions do not need a semi-
colon to end.
• There are different forms of macro substitution. The most common forms are :
• 1) Simple macro substitution
• 2) Argumented macro substitution
• 3) Nested macro substitution
• Note that there is no space between the identifier and the left parentheses. The identifiers
f1,f2,f3,---fn are the formal macro arguments that analogous to the formal arguments in a
function definition.
return 0;
}
• Ex:
• #define SIZE 10
• --------------
• #undefine SIZE
• #define SIZE 20
Command Meaning
__DATE__ Provides String “mm dd yyyy” format
__FILE__ Provides String constant containing name of source file
including path
__LINE__ Provides integer constant, containing current statement
OFF (exclude)a particular line (or) group of lines in a program. There are 6 types:
1. Two-way command
2. Multi-way commands
3. Line commands
4. Error commands
5. Null commands
6. Pragma commands.
• #if expression1
• #elif expression2
• #else
• #endif
• Set the next line of the program to linenumber+1 and create a program
name that is checked by predefined macrocall –FILE—
• Otherwise, “else” clause statements are included in source file for compilation and execution.
• #include <stdio.h>
• int main()
• {
• #ifdef RAJU
• "this C file\n");
• #else
• #endif
• return 0;
• }
• Syntax: #
• This is considered as null command and does not generate a compilation error.