Wednesday, July 26, 2023

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the list. 

The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it with the first element of the unsorted part. This process is repeated for the remaining unsorted portion until the entire list is sorted. 

Souce Code:-

#include<stdio.h>

void SelectionSort(int a[], int n) {
    int i, j, small, temp;
    for (i=0; i<n-1; i++){
        small = i;
        for(j=i+1; j<n; j++){
            if (a[j] < a[small]){
                small = j;
            }
        }
        temp = a[i];
        a[i] = a[small];
        a[small] = temp;
    }
}

int main() {
    int n;
    printf("Enter the length of the array : ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements : ");
    for (int i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    SelectionSort(arr, n);
    printf("The sorted array is :\n");
    for (int i=0; i<n; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:-
Enter the length of the array : 5 Enter the elements : 7 3 9 4 1 The sorted array is : 1 3 4 7 9

Insertion Sort Using C programming.

 Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.


Insertion Sort Algorithm

To sort an array of size N in ascending order iterate over the array and compare the current element (key) to its predecessor, if the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

Source Code:-

#include<stdio.h>

void InsertionSort(int a[], int n){
    int i, j, temp;
    for (int i=1; i<n; i++){
        temp = a[i];
        j=i-1;
        while(j>=0 && a[j]>temp){
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = temp;
    }
}

int main() {
    int n;
    printf("Enter the length of the array : ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements : ");
    for (int i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    InsertionSort(arr, n);
    printf("The sorted array is :\n");
    for (int i=0; i<n; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}

Output :-
Enter the length of the array : 5 Enter the elements : 4 7 3 1 9 The sorted array is : 1 3 4 7 9

Bubble Sort Using C programming.

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.


Bubble Sort Algorithm

In this algorithm, 

i. traverse from left and compare adjacent elements and the higher one is placed at right side. 

ii. In this way, the largest element is moved to the rightmost end at first. 

iii. This process is then continued to find the second largest and place it and so on until the data is sorted.


 Source Code:-

#include<stdio.h>

void BubbleSort(int *a, int n) {
    int i, j, temp;
    for (i=0; i<n-1; i++) {
        for (j=0; j<n-i-1; j++) {
            if (a[j] > a[j+1]) {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
}

int main() {
    int n;
    printf("Enter the length of the array : ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements : ");
    for (int i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    BubbleSort(arr, n);
    printf("The sorted array is :\n");
    for (int i=0; i<n; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}

output:
Enter the length of the array : 5
Enter the elements : 7 3 9 1 10
The sorted array is :
1 3 7 9 10

Saturday, March 18, 2023

C Programming. Program to merge two arrays and get an ordered array.

 Program to merge two arrays and get an ordered array.

// Program to merge two array and get an ordered array.

#include<stdio.h>

int main() {
    int n1, n2, i, j, t;
    printf("Enter the size of 1st array : ");
    scanf("%d", &n1);
    printf("Enter the size of 2nd array : ");
    scanf("%d", &n2);
    int a[n1], b[n2], c[n1+n2];
    printf("Enter the elements of 1st array in ordered manner : \n");
    for (i=0; i<n1; i++){
        scanf("%d",&a[i]);
    }
    printf("Enter the elements of 2nd array in ordered manner : \n");
    for (i=0; i<n2; i++){
        scanf("%d",&b[i]);
    }
    i=0;
    while (i<n1){
        c[i]=a[i];
        i++;
    }
    j=0;
    while (j<n2){
        c[i]=b[j];
        i++;
        j++;
    }
    for (i=0; i<n1+n2; i++){
        for (j=i; j<n1+n2; j++){
            if (c[i]>c[j]){
                t=c[i];
                c[i]=c[j];
                c[j]=t;
            }
        }
    }
    printf("After merging the array is :\n");
    for (i=0; i<n1+n2; i++){
        printf("%d ",c[i]);
    }
    return 0;
}

Output :

Enter the size of 1st array : 4 Enter the size of 2nd array : 5 Enter the elements of 1st array in ordered manner : 1 3 5 7 Enter the elements of 2nd array in ordered manner : 2 4 6 8 9 After merging the array is : 1 2 3 4 5 6 7 8 9

C Programming. Program to swap two numbers using macros.

 Program to swap two numbers using macros.

// Program to swap two numbers using macros.

#include<stdio.h>

#define swap(a, b) int t=a; a=b; b=t;

int main() {
    int a, b;
    printf("Enter two numbers : ");
    scanf("%d%d", &a, &b);
    printf("Before swapping a = %d, b = %d\n", a, b);
    swap(a, b);
    printf("After swapping a = %d, b = %d", a, b);
    return 0;
}

Output :

Enter two numbers : 5 7 Before swapping a = 5, b = 7 After swapping a = 7, b = 5

Friday, March 17, 2023

C Programming. Program to copy the content of one file to another.

 Program to copy the content of one file to another.

//  Program to copy the content of one file to another.

#include<stdio.h>

int main() {
    FILE *f1, *f2;
    char c;
    f1 = fopen("File1.txt", "r");
    f2 = fopen ("File2.txt", "w");
    while ((c=getc(f1))!=EOF){
        putc(c, f2);
    }
    fclose(f1);
    fclose(f2);
    return 0;
}

Output :

File1:
Hello guys, This Program is to copy the content of file 1 to file 2.
File2:
Hello guys, This Program is to copy the content of file 1 to file 2.

C Programming. Allocate memory dynamically using malloc() / calloc() functions.

Program to find sum and average of n elements entered by the user. Allocate memory dynamically using malloc() / calloc() functions.

// Program to find sum and average of n elements entered by the user.
// Allocate memory dynamically using malloc()/calloc() functions.

#include<stdio.h>
#include<stdlib.h>

int main() {
    int n, i, sum=0;
    float avg;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    int *p = (int *)calloc(n, sizeof(int));

    for (i=0; i<n; i++){
        printf("\nEnter the %dth element : ", i+1);
        scanf("%d", &p[i]);
    }
    for (i=0; i<n; i++){
        sum += p[i];
    }
    avg = (float)sum / n;
    printf("\nThe sum of %d elements is : %d\nAverage is : %0.2f ", n, sum, avg);

    return 0;
}

Output :

Enter the number of elements : 5 Enter the 1th element : 4 Enter the 2th element : 3 Enter the 3th element : 5 Enter the 4th element : 7 Enter the 5th element : 3 The sum of 5 elements is : 22 Average is : 4.40

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...