0% found this document useful (0 votes)
35 views

Program 8 15

The documents contain programs to sort lists using different sorting algorithms like selection sort, bubble sort, insertion sort, etc. and to search elements using linear search and binary search. The programs take input, sort/search and output the results.

Uploaded by

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

Program 8 15

The documents contain programs to sort lists using different sorting algorithms like selection sort, bubble sort, insertion sort, etc. and to search elements using linear search and binary search. The programs take input, sort/search and output the results.

Uploaded by

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

8. Program to read the names of cities and arrange them alphabetically.

#include <stdio.h>
#include <string.h>

int main() {
char s[100][100],temp[100];
int i,j,n;

printf("enter the size\n");


scanf("%d",&n);
printf("enter the cities \n");
for(i=1;i<=n;i++)
scanf("%s", s[i]);
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
if(strcmp(s[i],s[j])>0)
{strcpy(temp,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],temp);
}
printf(" the Sorted cities \n");
for(i=1;i<=n;i++)
printf("%s\n", s[i]);
return 0;
}
enter the size
4
enter the cities
bangalore
cochin
agra
delhi
the Sorted cities
agra
bangalore
cochin
delhi
9. Program to sort the given list using selection sort technique.
#include <stdio.h>
#include<conio.h>
// Function to swap two elements
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Function to perform selection sort
void selectionSort(int arr[], int n) {
int i, j, min;

// One by one move boundary of unsorted subarray


for (i = 0; i < n-1; i++) {
// Find the minimum element in unsorted array
min = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min])
min = j;
}

// Swap the found minimum element with the first element


swap(&arr[min], &arr[i]);
}
}

void main() {
int n ,i,arr[100];
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

selectionSort(arr, n);

printf("\nSorted array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
getch();
}
Enter the number of elements: 6
Enter 6 elements:
12
7
34
5
89
1
Original array:
12 7 34 5 89 1
Sorted array:
1 5 7 12 34 89
10. Program to sort the given list using bubble sort technique.
#include <stdio.h>

void bubbleSort(int arr[], int n) {


int i, j, temp;
for (i = 0; i < n-1; i++) {
// Last i elements are already in place
for (j = i+1; j < n; j++) {
if (arr[i] > arr[j]) {
// Swap arr[j] and arr[j+1]
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

void main() {
int n ,i,arr[100];
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

bubbleSort(arr, n);

printf("\nSorted array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
getch();
}

Part-B
1. Program to sort the given list using insertion sort technique. Replace technique with algorithm
#include <stdio.h>

// Function to perform insertion sort


void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void main() {
int n ,i,arr[100];
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

insertionSort(arr, n);

printf("\nSorted array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
getch();
}

2. Program to sort the given list using quick sort technique.


#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}

temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main(){
int i, count, number[25];

printf("How many elements are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
3. Program to sort the given list using merge sort technique.

#include <stdio.h>

void merge(int A[], int mid, int low, int high)


{
int i, j, k, B[100];
i = low;
j = mid + 1;
k = low;

while (i <= mid && j <= high)


{
if (A[i] < A[j])
{
B[k] = A[i];
i++;
k++;
}
else
{
B[k] = A[j];
j++;
k++;
}
}
while (i <= mid)
{
B[k] = A[i];
k++;
i++;
}
while (j <= high)
{
B[k] = A[j];
k++;
j++;
}
for (int i = low; i <= high; i++)
{
A[i] = B[i];
}

void mergeSort(int A[], int low, int high){


int mid;
if(low<high){
mid = (low + high) /2;
mergeSort(A, low, mid);
mergeSort(A, mid+1, high);
merge(A, mid, low, high);
}
}
void main() {
int n ,i,arr[100];
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

mergeSort(arr,0, n-1);

printf("\nSorted array: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
getch();
}
4. Program to search an element using linear search technique.
// Online C compiler to run C program online
#include <stdio.h>

void main()
{

int num;

int i, key, found = 0;

printf("Enter number of elements you would like to take as input: ");

scanf("%d", &num);

int arr[num];

printf("\nEnter all the elements of your choice:");

for (i = 0; i < num; 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 < num ; i++)

if (key == arr[i] )

found = 1;

break;

if (found == 1)

printf("we got the element at index %d",i+1);

else

printf("Elenment not found\n");

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

Enter all the elements of your choice:4


5
11
88
4
22

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


we got the element at index 4
5. Program to search an element using recursive binary search technique.
#include <stdio.h>

// Recursive function to perform binary search


int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
return binarySearch(arr, mid + 1, high, x);
}
return -1; // Return -1 if element is not found
}

int main() {
int n, x;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];

printf("Enter %d elements in sorted order:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the element to search: ");


scanf("%d", &x);

int index = binarySearch(arr, 0, n - 1, x);


if (index != -1) {
printf("Element %d found at index %d.\n", x, index);
} else {
printf("Element %d not found.\n", x);
}
return 0;
}
Enter the number of elements: 5
Enter 5 elements in sorted order:
22
33
44
55
66
Enter the element to search: 11
Element 11 not found

You might also like