0% found this document useful (0 votes)
2 views6 pages

Ds Programs

The document provides C programs for various array manipulations, including reversing an array, implementing linear and binary search algorithms, and sorting techniques such as insertion sort, selection sort, and bubble sort. Each section includes code snippets and example outputs demonstrating the functionality of the programs. The programs allow users to input array sizes and elements, performing operations like searching for specific elements and sorting arrays.

Uploaded by

preeti.gudapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Ds Programs

The document provides C programs for various array manipulations, including reversing an array, implementing linear and binary search algorithms, and sorting techniques such as insertion sort, selection sort, and bubble sort. Each section includes code snippets and example outputs demonstrating the functionality of the programs. The programs allow users to input array sizes and elements, performing operations like searching for specific elements and sorting arrays.

Uploaded by

preeti.gudapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Exercise 1: Array Manipulation

i) Write a program to reverse an array.


#include<stdio.h>
int main()
{
int n, arr[n], i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int rev[n], j = 0;
for(i = n-1; i >= 0; i--)
{
rev[j] = arr[i];
j++;
}
printf("The Reversed array: ");
for(i = 0; i < n; i++)
{
printf("%d ", rev[i]);
}
}
OUTPUT:
Enter the size of the array: 7
Enter the elements: 9
6
12
3
15
25
7
The Reversed array: 7 25 15 3 12 6 9
ii) C Programs to implement the Searching Techniques – Linear & Binary Search
a) Linear Search
#include <stdio.h>
int main()
{
int n,i, key, element_found = 0;
printf("Enter number of elements you would like to take as input: ");
scanf("%d", &n);
int arr[n];
printf("\nEnter all the elements of your choice:");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter the key element that you would like to be searched: ");
scanf("%d", &key);
/* Linear search starts */
for (i = 0; i < n; i++)
{
if (key == arr[i] )
{
element_found = 1;
break;
}
}
if (element_found == 1)
printf("we got the element at index %d",i+1);
else
printf("we haven’t got element at any index in the array\n");
return 0;
}
OUTPUT:
Enter number of elements you would like to take as input: 5
Enter all the elements of your choice:4
3
6
5
8
Enter the key element that you would like to be searched: 6
we got the element at index 3

Enter number of elements you would like to take as input: 4

Enter all the elements of your choice:6


3
7
4

Enter the key element that you would like to be searched: 9


we havenÆt got element at any index in the array
b) Binary Search
#include <stdio.h>
int main()
{
int a[100],i,low,high,mid,se,n,found=0;
printf("\n Enter array size: ");
scanf("%d",&n);
printf("\n Enter %d Elements \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter search element :");
scanf("%d",&se);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(se==a[mid])
{
printf("\n search element found at location %d",mid);
found=1;
break;
}
else if (se<a[mid])
{
high=mid-1;
}
else if (se>a[mid])
{
low=mid+1;
}
}
if(found==0)
{
printf("\n search element not found");
}
return 0;
}
OUTPUT:
Enter array size: 5
Enter 5 Elements
8
25
9
11
6
Enter search element :11
search element found at location 3
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort
a) Insertion Sort

#include <stdio.h>
int main()
{
int a[100],i,n,j,key;
printf("\n Enter array size: ");
scanf("%d",&n);
printf("\n Enter %d Elements \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf(“\n Before sorting array elements are\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
for(i=0;i<n;i++)
{
key=a[i];
j=i-1;
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
printf(“\n after sorting array elements are”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
}
OUTPUT:
Enter array size: 8

Enter 8 Elements
6
8
24
7
61
15
3
4

Before sorting array elements are


6 8 24 7 61 15 3 4
after sorting array elements are3 4 6 7 8 15 24 61
b) Selection sort
#include<stdio.h>
#include<string.h>
int main()
{
int a[100],i,n,j,min,temp;
printf("\n Enter array size: ");
scanf("%d",&n);
printf("\n Enter %d Elements \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Before sorting array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("\n after sorting array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
Enter array size: 6
Enter 6 Elements
8
6
12
5
35
25
Before sorting array elements are
8 6 12 5 35 25
after sorting array elements are5 6 8 12 25 35
c) Bubble sort
#include<stdio.h>
#include<string.h>
int main()
{
int a[100],i,n,j,temp;
printf("\n Enter array size: ");
scanf("%d",&n);
printf("\n Enter %d Elements \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Before sorting array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;i++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n after sorting array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
Enter array size: 5

Enter 5 Elements
9
5
7
6
8

Before sorting array elements are:


9 5 7 6 8
after sorting array elements are:
5 9 7 6 8

You might also like