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

Programs - Searching&sorting of C

The document contains 6 programs that implement different sorting algorithms in C: 1. A binary search program that uses an iterative approach to search for an element in a sorted array. 2. A bubble sort program that sorts an array of integers in ascending order by repeatedly swapping adjacent elements. 3. An insertion sort program that inserts elements into a sorted subset of the array one by one. 4. A selection sort program that finds the minimum element from the unsorted sublist and swaps it with the first element. 5. A quicksort program that uses partitioning and recursion to sort an array of integers. 6. All programs take input from the user, sort the data structures, and

Uploaded by

Razeen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Programs - Searching&sorting of C

The document contains 6 programs that implement different sorting algorithms in C: 1. A binary search program that uses an iterative approach to search for an element in a sorted array. 2. A bubble sort program that sorts an array of integers in ascending order by repeatedly swapping adjacent elements. 3. An insertion sort program that inserts elements into a sorted subset of the array one by one. 4. A selection sort program that finds the minimum element from the unsorted sublist and swaps it with the first element. 5. A quicksort program that uses partitioning and recursion to sort an array of integers. 6. All programs take input from the user, sort the data structures, and

Uploaded by

Razeen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Searching and sorting programs

1. PROGRAM TO IMPLEMENT BINARY SEARCH USING ITERATIVE CALL

#include <stdio.h>

int iterativeBinarySearch(int array[], int start_index, int end_index, int element){

   while (start_index <= end_index){

      int middle = start_index + (end_index- start_index )/2;

      if (array[middle] == element)

         return middle;

      if (array[middle] < element)

         start_index = middle + 1;

      else

         end_index = middle - 1;

   }

   return -1;

int main(void){

   int array[] = {1, 4, 7, 9, 16, 56, 70};

   int n = 7;

   int element = 16;

   int found_index = iterativeBinarySearch(array, 0, n-1, element);

   if(found_index == -1 ) {

      printf("Element not found in the array ");

   }

   else {

      printf("Element found at index : %d",found_index);

   }

   return 0;
}
Output
Element found at index: 4

3. Bubble sort program in C


/* Bubble sort code */

#include <stdio.h>

int main()
{
  int array[100], n, c, d, swap;

  printf("Enter number of elements\n");


  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < n - 1; c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for (c = 0; c < n; c++)
     printf("%d\n", array[c]);

  return 0;
}
Output of program:

4. Insertion sort program in C


/* Insertion sort ascending order */

#include <stdio.h>
int main()
{
  int n, array[1000], c, d, t, flag = 0;
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
  for (c = 1 ; c <= n - 1; c++) {
    t = array[c];
    for (d = c - 1 ; d >= 0; d--) {
      if (array[d] > t) {
        array[d+1] = array[d];
        flag = 1;
      }
      else
        break;
    }
    if (flag)
      array[d+1] = t;
  }
  printf("Sorted list in ascending order:\n");
  for (c = 0; c <= n - 1; c++) {
    printf("%d\n", array[c]);
  }
  return 0;
}
Output of program:

5. Selection sort program in C


#include <stdio.h>

int main()
{
  int array[100], n, c, d, position, t;

  printf("Enter number of elements\n");


  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times


  {
    position = c;

    for (d = c + 1; d < n; d++)
    {
      if (array[position] > array[d])
        position = d;
    }
    if (position != c)
    {
      t = array[c];
      array[c] = array[position];
      array[position] = t;
    }
  }

  printf("Sorted list in ascending order:\n");


  for (c = 0; c < n; c++)
    printf("%d\n", array[c]);

  return 0;
}

Output of program:

6.Quick sort program in C


#include <stdio.h>
#include <stdbool.h>

#define MAX 7

int intArray[MAX] = {4,6,3,2,1,9,7};

void printline(int count) {


int i;

for(i = 0;i < count-1;i++) {


printf("=");
}

printf("=\n");
}
void display() {
int i;
printf("[");

// navigate through all items


for(i = 0;i < MAX;i++) {
printf("%d ",intArray[i]);
}

printf("]\n");
}

void swap(int num1, int num2) {


int temp = intArray[num1];
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}

int partition(int left, int right, int pivot) {


int leftPointer = left -1;
int rightPointer = right;

while(true) {
while(intArray[++leftPointer] < pivot) {
//do nothing
}

while(rightPointer > 0 && intArray[--rightPointer] > pivot) {


//do nothing
}

if(leftPointer >= rightPointer) {


break;
} else {
printf(" item swapped :%d,%d\n", intArray[leftPointer],intArray[rightPointer]);
swap(leftPointer,rightPointer);
}
}

printf(" pivot swapped :%d,%d\n", intArray[leftPointer],intArray[right]);


swap(leftPointer,right);
printf("Updated Array: ");
display();
return leftPointer;
}

void quickSort(int left, int right) {


if(right-left <= 0) {
return;
} else {
int pivot = intArray[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
}
}

int main() {
printf("Input Array: ");
display();
printline(50);
quickSort(0,MAX-1);
printf("Output Array: ");
display();
printline(50);
}
If we compile and run the above program, it will produce the following result −

Output:
Input Array: [4 6 3 2 1 9 7 ]
====================

You might also like