# Data Structure – codewindow.in

## Data Structure

#### In C, a one-dimensional array is a collection of elements of the same data type that are stored in contiguous memory locations. It is declared and initialized as follows:

``````data_type array_name[size];
``````

#### 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};
``````

#### A two-dimensional array in C is an array of arrays. It is declared and initialized using the following syntax:

``````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}
};
``````

#### In C, elements in a two-dimensional array can be accessed using the row and column indices. The syntax for accessing an element in a 2D array is:

``````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
``````

#### Here is an example of how to traverse an array in C:

``````#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
``````

#### To reverse an array in C, you can swap the first element with the last element, the second element with the second to last element, and so on until the middle of the array is reached. Here's an example code:

``````#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
``````

#### Here is an example implementation of bubble sort in C:

``````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;
}
}
}
}
``````

#### Here's an implementation of selection sort in C:

``````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);
``````

#### Here is an example of how to sort an array in C using insertion sort:

``````#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;
}
``````

#### Here is an example implementation of quick sort in C:

``````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;
}
``````

#### To find the maximum and minimum elements in an array in C, you can iterate over the array and keep track of the maximum and minimum elements seen so far. Here's an example code that does this:

``````#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;
}
``````

## We Loveto Support you

Go through our study material. Your Job is awaiting.