Codes for lab

 Bubble sort..


// C program for implementation of Bubble sort

#include <stdio.h>


void swap(int* arr, int i, int j) {

    int temp = arr[i];

    arr[i] = arr[j];

    arr[j] = temp;

}


void bubbleSort(int arr[], int n) {

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

      

        // Last i elements are already in place, so the loop

        // will only num n - i - 1 times

        for (int j = 0; j < n - i - 1; j++) {

            if (arr[j] > arr[j + 1])

                swap(arr, j, j + 1);

        }

    }

}


int main() {

    int arr[] = { 6, 0, 3, 5 };

    int n = sizeof(arr) / sizeof(arr[0]);


    // Calling bubble sort on array arr

    bubbleSort(arr, n);


    for (int i = 0; i < n; i++)

        printf("%d

 ", arr[i]);


    return 0;

}






Insertion short 


// C++ program for implementation of Insertion Sort

#include <iostream>

using namespace std;


/* Function to sort array using insertion sort */

void insertionSort(int arr[], int n)

{

    for (int i = 1; i < n; ++i) {

        int key = arr[i];

        int 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] > key) {

            arr[j + 1] = arr[j];

            j = j - 1;

        }

        arr[j + 1] = key;

    }

}


/* A utility function to print array of size n */

void printArray(int arr[], int n)

{

    for (int i = 0; i < n; ++i)

        cout << arr[i] << " ";

    cout << endl;

}


// Driver method

int main()

{

    int arr[] = { 12, 11, 13, 5, 6 };

    int n = sizeof(arr) / sizeof(arr[0]);


    insertionSort(arr, n);

    printArray(arr, n);


    return 0;

}





Selection short


// C++ program to implement Selection Sort

#include <bits/stdc++.h>

using namespace std;


void selectionSort(vector<int> &arr) {

    int n = arr.size();


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


        // Assume the current position holds

        // the minimum element

        int min_idx = i;


        // Iterate through the unsorted portion

        // to find the actual minimum

        for (int j = i + 1; j < n; ++j) {

            if (arr[j] < arr[min_idx]) {


                // Update min_idx if a smaller

                // element is found

                min_idx = j; 

            }

        }


        // Move minimum element to its

        // correct position

        swap(arr[i], arr[min_idx]);

    }

}


void printArray(vector<int> &arr) {

    for (int &val : arr) {

        cout << val << " ";

    }

    cout << endl;

}


int main() {

    vector<int> arr = {64, 25, 12, 22, 11};


    cout << "Original array: ";

    printArray(arr); 


    selectionSort(arr);


    cout << "Sorted ar

ray: ";

    printArray(arr);


    return 0;

}




Inserting a particular element at a particular position in an array.

#include <iostream>

using namespace std;


int main() {

    int size, element, position;


    // Enter the size of the array

    cout << "Enter the size of the array: ";

    cin >> size;


    int arr[size + 1]; // Array with an extra space for the new element


    // Input the elements of the array

    cout << "Enter the elements of the array:\n";

    for(int i = 0; i < size; i++) {

        cin >> arr[i];

    }


    // Enter the element to be inserted and the position

    cout << "Enter the element to be inserted: ";

    cin >> element;

    cout << "Enter the position to insert the element (0-based index): ";

    cin >> position;


    // Check if the position is valid

    if(position < 0 || position > size) {

        cout << "Invalid position!" << endl;

        return 1;

    }


    // Shift elements to the right

    for(int i = size; i > position; i--) {

        arr[i] = arr[i - 1];

    }


    // Insert the element at the given position

    arr[position] = element;


    // Display the updated array

    cout << "Array after insertion:\n";

    for(int i = 0; i <= size; i++) {

        cout << arr[i] << " ";


    }

    cout << endl;


    return 0;

}





Deletion at a particular position in an array.


#include <iostream>

using namespace std;


int main() {

    int size, position;


    // Enter the size of the array

    cout << "Enter the size of the array: ";

    cin >> size;


    int arr[size]; // Array with the specified size


    // Input the elements of the array

    cout << "Enter the elements of the array:\n";

    for(int i = 0; i < size; i++) {

        cin >> arr[i];

    }


    // Enter the position to delete the element

    cout << "Enter the position to delete the element (0-based index): ";

    cin >> position;


    // Check if the position is valid

    if(position < 0 || position >= size) {

        cout << "Invalid position!" << endl;

        return 1;

    }


    // Shift elements to the left from the position specified

    for(int i = position; i < size - 1; i++) {

        arr[i] = arr[i + 1];

    }


    // Display the updated array

    cout << "Array after deletion:\n";

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

        cout << arr[i] << " ";

   

 }

    cout << endl;


    return 0;

}



KRISHNA

Author & Editor

Thanks for visiting xmonocodes , i hope you are getting everything you want.

0 comments:

Post a Comment