L-10: Arrays, 1D 2D problems

L-10 Arrays, 1D 2D problems #


alt text


Insertion Sort

#include<stdio.h>
int main(){
  int arr[] = { 9, 6, 7, 2, 5, 8};
  int size = sizeof(arr) / sizeof(arr[0]);
  int i,j;
for (i = 1; i < size; i++) { // Start from 1 as arr[0] is always sorted
    int currentElement = 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] > currentElement) {
      arr[j + 1] = arr[j];
      j = j - 1;}
     // Finally place the Current element at its correct position.
    arr[j + 1] = currentElement;
  }
  printf("Sorted Array: ");
  for (i = 0; i < size; i++){
    printf("%d ", arr[i]);}
  printf("\n");
  return 0;
      }

Bubble Sort

#include<stdio.h>
int main()
{
  int arr[] = {3,2,6,5,4,7,8,9,10,1};
  int size = sizeof(arr)/sizeof(arr[0]);
  // loop over array elements
  for (int i = 0; i < size - 1; ++i) {
    // swapped variable initially set to 0  
    int swapped = 0;
    // loop to compare array elements
    for (int j = 0; j < size - i - 1; ++j) {
      // compare adjacent elements
      if (arr[j] > arr[j + 1]) {
        // swap if out-of-order
        int temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
        swapped = 1;
      }
    }
    if(swapped == 0) { break;}
  }
  printf("Sorted Array: ");
  for(int i = 0; i < size; i++)  {
        printf(" %d", arr[i]);
    }
  printf("\n");
  return 0;
        }

Selection Sort

#include <stdio.h>
int main()
{ int n = 10;
  int a[] = {3,2,6,5,4,7,8,9,10,1};
  int min_index;
  for(int i = 0; i < n - 1; i++) {
    min_index = i;
    for(int j = i + 1; j < n; j++) 
    { if(a[min_index] > a[j]) {
          min_index = j;
        }
    }
    if(min_index != i)
    { int temp = a[i];
      a[i] = a[min_index];
      a[min_index] = temp;
    }
  }
  printf("Sorted Array: ");
  for(int i = 0; i < n; i++)  {
    printf(" %d", a[i]);}
  printf("\n");
  return 0;
            }

Leader in array

#include<stdio.h>
int main()
{
    int arr[] = {21, 16, 17, 4, 6, 3, 5, 2};
    int n = sizeof(arr)/sizeof(arr[0]); // get the array length
    int max_from_right =  arr[n-1];
    printf("%d ", max_from_right);
    for (int i = n-2; i >= 0; i--)
    {
        if (max_from_right < arr[i])
        {          
            max_from_right = arr[i];    // 
            printf("%d ",max_from_right);
        }
    }
    printf("\n");
    return 0;
    } 

Insertion Sort: https://courses.iiit.ac.in/mod/resource/view.php?id=55407 Bubble Sort: https://courses.iiit.ac.in/mod/resource/view.php?id=55408 Selection Sort: https://courses.iiit.ac.in/mod/resource/view.php?id=55409 Leader in Array: https://courses.iiit.ac.in/mod/resource/view.php?id=55410

Reading Material:


Practice problems 1 #

  • Write a C program to read and print elements of array.
  • Write a C program to find sum of all array elements.
  • Write a C program to find maximum and minimum element in an array.
  • Write a C program to print all negative elements in an array.
  • Write a C program to count total number of even and odd elements in an array.
  • Write a C program to count total number of negative elements in an array.

Practice problems 2 #

  • Write a C program to copy all elements from an array to another array
  • Write a C program to insert an element in an array.
  • Write a C program to find reverse of an array.
  • Write a C program to merge two array to third array.
  • Write a C program to count total number of duplicate elements in an array.
  • Write a C program to print all unique elements in the array.
  • Write a C program to count frequency of each element in an array.
  • Write a C program to put even and odd elements of array in two separate array.

Practice Problem 3 #

1000 simple problems topic wise

L-10, Slides #