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

Dsalaab

Uploaded by

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

Dsalaab

Uploaded by

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

linear search

#include <stdio.h>

int main()
{
int arr[20]; // Array to hold up to 20 elements
int i, size, searchElement;

// Prompt user for number of elements


printf("\n\t-- Linear Search --\n\n");
printf("Enter total number of elements (max 20): ");
scanf("%d", &size);

// Ensure size does not exceed array bounds


if (size > 20 || size <= 0) {
printf("Invalid size! Please enter a number between 1 and 20.\n");
return 1; // Exit the program with an error code
}

// Input elements into the array


for (i = 0; i < size; i++)
{
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

// Prompt user for the element to search


printf("Enter the element to be searched: ");
scanf("%d", &searchElement);

// Perform linear search


for (i = 0; i < size; i++)
{
if (searchElement == arr[i])
{
printf("Element found at position: %d\n", i + 1);
return 0; // Exit after finding the element
}
}

// If the element is not found


printf("Element not found in the list.\n");
return 0; // Exit the program successfully
}

output

Enter total number of elements (max 20): 20


Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Enter element 6: 6
Enter element 7: 7
Enter element 8: 8
Enter element 9: 9
Enter element 10: 10
Enter element 11: 11
Enter element 12: 12
Enter element 13: 13
Enter element 14: 14
Enter element 15: 15
Enter element 16: 16
Enter element 17: 17
Enter element 18: 18
Enter element 19: 19
Enter element 20: 20
Enter the element to be searched: 18
Element found at position: 18

binary search

#include <stdio.h>
#include <conio.h> // Required for getch() in Turbo C++

int main() {
int n, search, found = 0;
int low, high, mid;
int a[20]; // Array to hold up to 20 elements

// Clear the screen (optional in Turbo C++)


clrscr();

// Prompt user for the number of elements


printf("Enter the number of elements (up to 20): ");
scanf("%d", &n);

// Ensure the number of elements does not exceed the array size
if (n > 20 || n <= 0) {
printf("Invalid number of elements! Please enter a number between 1 and
20.\n");
getch(); // Wait for user input before exiting
return 1; // Exit with an error code
}

// Input elements into the array


printf("Enter %d elements in ascending order:\n", n);
for (int i = 0; i < n; i++) {
printf("a[%d] = ", i);
scanf("%d", &a[i]);
}

// Prompt user for the element to search


printf("Enter the element to search: ");
scanf("%d", &search);

// Initialize low and high for binary search


low = 0; // Change to 0 for 0-based indexing
high = n - 1; // Change to n - 1 for 0-based indexing

// Perform binary search


while (low <= high) {
mid = (low + high) / 2;

if (search < a[mid]) {


high = mid - 1; // Search in the left half
} else if (search > a[mid]) {
low = mid + 1; // Search in the right half
} else {
found = 1; // Element found
printf("Element found at position: %d\n", mid + 1); // Display position
(1-based)
break; // Exit loop if found
}
}

// If the element is not found


if (!found) {
printf("Element not present in the array.\n");
}

getch(); // Wait for user input before exiting


return 0; // Exit program successfully
}

output

Enter the number of elements (up to 20): 6


Enter 6 elements in ascending order:
a[0] = 2
a[1] = 3
1a[2] = 0
a[3] = 34
a[4] = 87

a[5] = 798
Enter the element to search: 87
Element found at position: 5

=== Code Execution Successful ===

bubble sort

#include <stdio.h>
#include <conio.h>

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

// Prompt user for the total number of integers


printf("Enter the total integers you want to enter (make it less than 100):\
n");
scanf("%d", &n);

// Validate the number of integers


if (n <= 0 || n > 100) {
printf("Please enter a valid number of integers (1 to 99).\n");
getch(); // Wait for user input before exiting
return 1; // Exit with an error code
}

// Prompt user for array elements


printf("Enter the %d integer array elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

// Bubble sort algorithm


for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) { // Compare adjacent elements
// Swap if they are in the wrong order
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}

// Print the sorted numbers


printf("The sorted numbers are:");
for (i = 0; i < n; i++) {
printf("%3d", a[i]);
}
printf("\n"); // New line for better output formatting

getch(); // Wait for user input before exiting


return 0; // Exit program successfully
}

output

Enter the total integers you want to enter (make it less than 100):
5
Enter the 5 integer array elements:
34
12
5
78
23
The sorted numbers are: 5 12 23 34 78

quick sort

#include <stdio.h>
#include <conio.h>

void quickSort(int arr[], int first, int last);

int main() {
int arr[30]; // Array to hold up to 30 elements
int size;
// Prompt user for the total number of elements
printf("Enter total number of elements (up to 30): ");
scanf("%d", &size);

// Validate the size input


if (size <= 0 || size > 30) {
printf("Please enter a valid number of elements (1 to 30).\n");
getch(); // Wait for user input before exiting
return 1; // Exit with an error code
}

// Prompt user for array elements


printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Call the Quick Sort function


quickSort(arr, 0, size - 1);

// Print the sorted elements


printf("Sorted elements are:\n");
for (int i = 0; i < size; i++) {
printf("%d\t", arr[i]);
}
printf("\n"); // New line for better output formatting

getch(); // Wait for user input before exiting


return 0; // Exit program successfully
}

void quickSort(int arr[], int first, int last) {


if (first < last) {
int pivot = first; // Choosing the first element as pivot
int i = first;
int j = last;
while (i < j) {
// Move i to the right until an element larger than the pivot is found
while (arr[i] <= arr[pivot] && i < last) {
i++;
}
// Move j to the left until an element smaller than the pivot is found
while (arr[j] > arr[pivot]) {
j--;
}
// Swap elements at i and j if they are in the wrong order
if (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap the pivot element with the element at j
int temp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = temp;

// Recursively sort the partitions


quickSort(arr, first, j - 1);
quickSort(arr, j + 1, last);
}
}

output

Enter total number of elements (up to 30):


5
Enter 5 elements:
34
12
5
78
23
Sorted elements are:
5 12 23 34 78

You might also like