Join Regular Classroom : Visit ClassroomTech

Data Structure – codewindow.in

Related Topics

Data Structure

data_type array_name[size];

where data_type is the type of data that the array will store, array_name is the name of the array, and size is the number of elements that the array will contain.

For example, to declare and initialize an array of integers with 5 elements, you would use the following code:

int my_array[5];

You can also initialize the array with values using the following syntax:

data_type array_name[size] = {value1, value2, value3, ..., valueN};

For example, to initialize an array of integers with 3 elements to the values 1, 2, and 3, you would use the following code:

int my_array[3] = {1, 2, 3};
data_type array_name[row_size][column_size] = {
    {data_type_1, data_type_2, ..., data_type_column_size},
    {data_type_1, data_type_2, ..., data_type_column_size},
    .
    .
    .
    {data_type_1, data_type_2, ..., data_type_column_size}
};

where data_type is the data type of the elements in the array, array_name is the name of the array, row_size is the number of rows in the array, and column_size is the number of columns in the array. The elements of the array are initialized using curly braces, with each row enclosed in its own set of curly braces. For example, the following code declares and initializes a 2D array with 3 rows and 4 columns:

int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
array_name[row_index][column_index]

For example, consider the following 2D array:

int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

To access the element in the second row and third column, you would write:

int x = arr[1][2]; // x now holds the value 6
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    
    return 0;
}

In this example, we have an integer array arr with five elements. We use the sizeof operator to get the size of the array and divide it by the size of each element to get the number of elements in the array. We then use a for loop to iterate through each element of the array and print its value using printf. The loop runs from i = 0 to i < n, where n is the size of the array, and increments i by 1 each time. The output of this program would be:

1 2 3 4 5
#include <stdio.h>

int main() {
   int arr[] = {1, 2, 3, 4, 5};
   int size = sizeof(arr) / sizeof(arr[0]);

   printf("Original array: ");
   for (int i = 0; i < size; i++) {
      printf("%d ", arr[i]);
   }

   for (int i = 0; i < size / 2; i++) {
      int temp = arr[i];
      arr[i] = arr[size - i - 1];
      arr[size - i - 1] = temp;
   }

   printf("\nReversed array: ");
   for (int i = 0; i < size; i++) {
      printf("%d ", arr[i]);
   }

   return 0;
}

Output:

Original array: 1 2 3 4 5 
Reversed array: 5 4 3 2 1 
void bubbleSort(int arr[], int n) {
    int i, j;
    for (i = 0; i < n-1; i++) {
        // Last i elements are already sorted
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                // Swap arr[j] and arr[j+1]
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

In this implementation, the bubbleSort function takes an integer array arr and its size n as parameters. It then uses two nested loops to traverse the array and compare adjacent elements. If a pair of adjacent elements is out of order, the function swaps them using a temporary variable temp. The sorting algorithm terminates when no more swaps are needed.

void selectionSort(int arr[], int n) {
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++) {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx]) {
                min_idx = j;
            }
        }

        // Swap the minimum element with the first element
        int temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}

You can call this function by passing the array and its size as arguments, like this:

int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);

After calling this function, the arr array will be sorted in ascending order.

#include <stdio.h>

void insertionSort(int arr[], int n) {
    int i, key, j;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

int main() {
    int arr[] = { 12, 11, 13, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    insertionSort(arr, n);
    printf("Sorted array: \n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

In the above example, we have defined a function insertionSort that takes an array arr and its size n as input. The function sorts the array using the insertion sort algorithm. We then call this function from the main function, passing the array and its size as arguments. Finally, we print the sorted array using a for loop.

void quick_sort(int arr[], int left, int right) {
    if (left < right) {
        int pivot_index = partition(arr, left, right);
        quick_sort(arr, left, pivot_index - 1);
        quick_sort(arr, pivot_index + 1, right);
    }
}

int partition(int arr[], int left, int right) {
    int pivot = arr[right];
    int i = left - 1;
    for (int j = left; j < right; j++) {
        if (arr[j] <= pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[right]);
    return i + 1;
}

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

In the quick_sort function, we first check if left < right to ensure that we have more than one element in the sub-array to sort. We then call the partition function to get the index of the pivot element and recursively apply the quick sort algorithm to the sub-arrays to the left and right of the pivot.

In the partition function, we choose the rightmost element of the sub-array as the pivot. We then iterate through the sub-array from left to right and swap elements that are less than or equal to the pivot with the element at the current index i. After iterating through the sub-array, we swap the pivot element with the element at index i + 1 to put the pivot element in its correct position in the sorted array. Finally, we return the index of the pivot element.

#include <stdio.h>

int main() {
   int arr[] = { 5, 3, 7, 1, 9, 4, 8, 2, 6 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int i, max, min;
   
   max = arr[0]; // assume first element as maximum
   min = arr[0]; // assume first element as minimum
   
   for(i = 1; i < n; i++) {
      if(arr[i] > max) {
         max = arr[i];
      }
      if(arr[i] < min) {
         min = arr[i];
      }
   }
   
   printf("Maximum element = %d\n", max);
   printf("Minimum element = %d\n", min);
   
   return 0;
}

In this example, the arr array is iterated over and the max and min variables are updated if a larger or smaller element is found, respectively. Finally, the max and min values are printed to the console.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories