Kamrul13981

Untitled

Jun 16th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. //Selection sort
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     int array[100], n, c, d, position, t;
  6.  
  7.     printf("Enter number of elements\n");
  8.     scanf("%d", &n);
  9.  
  10.     printf("Enter %d integers\n", n);
  11.  
  12.     for (c = 0; c < n; c++)
  13.         scanf("%d", &array[c]);
  14.  
  15.     for (c = 0; c < (n - 1); c++)
  16.     {
  17.         position = c;
  18.  
  19.         for (d = c + 1; d < n; d++)
  20.         {
  21.             if (array[position] > array[d])
  22.                 position = d;
  23.         }
  24.         if (position != c)
  25.         {
  26.             t = array[c];
  27.             array[c] = array[position];
  28.             array[position] = t;
  29.         }
  30.     }
  31.  
  32.     printf("Sorted list in ascending order:\n");
  33.  
  34.     for (c = 0; c < n; c++)
  35.         printf("%d\n", array[c]);
  36.  
  37.     return 0;
  38. }
  39.  
  40. //insertion sort
  41. #include <stdio.h>
  42.  
  43. int main()
  44. {
  45.   int n, array[1000], c, d, t, flag = 0;
  46.  
  47.   printf("Enter number of elements\n");
  48.   scanf("%d", &n);
  49.  
  50.   printf("Enter %d integers\n", n);
  51.  
  52.   for (c = 0; c < n; c++)
  53.     scanf("%d", &array[c]);
  54.  
  55.   for (c = 1 ; c <= n - 1; c++) {
  56.     t = array[c];
  57.  
  58.     for (d = c - 1 ; d >= 0; d--) {
  59.       if (array[d] > t) {
  60.         array[d+1] = array[d];
  61.         flag = 1;
  62.       }
  63.       else
  64.         break;
  65.     }
  66.     if (flag)
  67.       array[d+1] = t;
  68.   }
  69.  
  70.   printf("Sorted list in ascending order:\n");
  71.  
  72.   for (c = 0; c <= n - 1; c++) {
  73.     printf("%d\n", array[c]);
  74.   }
  75.  
  76.   return 0;
  77. }
  78.  
  79. //insertionSort And Binary search Using Function
  80. #include<stdio.h>
  81.  
  82. void insertionSort(int arr[], int n)
  83. {
  84.     int i, value, empty;
  85.  
  86.     for(i=1;i<n;i++){
  87.     value=arr[i];
  88.     empty=i;
  89.  
  90.     while(empty>0 && arr[empty-1]>value){
  91.         arr[empty]=arr[empty-1];
  92.         empty--;
  93.     }
  94.     arr[empty]=value;
  95. }
  96.  
  97. }
  98.  void binary_search( int arr[], int n,int item)
  99. {
  100.     int left =0, right= n-1, middle;
  101.         while(left<=right)
  102.         {
  103.           middle=(left+right)/2;
  104.  
  105.  
  106.             if(arr[middle]==item)
  107.             {
  108.                 printf("item found index %d" ,middle);
  109.                 return 0;
  110.             }
  111.         else if (arr[middle]<item)
  112.         {
  113.             left= middle+1;
  114.         }
  115.  
  116.         else {
  117.             right=middle-1;
  118.         }
  119.     }
  120.     printf("Item not found");
  121.  
  122. }
  123. int main()
  124. {
  125.     int n;
  126.     printf("Enter array size : ");
  127.     scanf("%d",&n);
  128.  
  129.     printf("Enter Array Elements : ");
  130.     int arr[n];
  131.     for(int i=0; i<n; i++)
  132.         {
  133.             scanf("%d",&arr[i]);
  134.         }
  135.         insertionSort(arr,n);
  136.         int key;
  137.         scanf("%d",&key);
  138.         binary_search(arr,n, key);
  139.         return 0;
  140.  
  141.  
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment