You are on page 1of 5

Array

Certainly! In C++, an array is a collection of elements of the same data type stored in contiguous memory
locations. Arrays provide a way to store and access multiple values using a single identifier. Each element
in an array is identified by an index, starting from 0.

Declaration and Initialization


Declaration:

• int numbers[5]; declares an integer array named numbers with a capacity to store 5 elements.

Initialization:

• int numbers[5] = {1, 2, 3, 4, 5}; declares and initializes an integer array named numbers with the
values 1, 2, 3, 4, and 5.

#include <iostream>
using namespace std;

int main() {
// Declaration and Initialization
int numbers[5] = {1, 2, 3, 4, 5};

// Accessing elements
cout << "First element: " << numbers[0] << endl;
cout << "Second element: " << numbers[1] << endl;

return 0;
}
2. Accessing Elements:
You can traverse the array element by using loop.

#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};

// Accessing elements
for (int i = 0; i < 5; ++i) {
cout << "Element " << i + 1 << ": " << numbers[i] << endl;
}

return 0;
}
3. Modifying Elements:

#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};

// Modifying elements
numbers[2] = 10;

// Accessing and displaying modified elements


for (int i = 0; i < 5; ++i) {
cout << "Element " << i + 1 << ": " << numbers[i] << endl;
}

return 0;
}

4. Array Size and Iteration:

#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};

// Array size
int size = sizeof(numbers) / sizeof(numbers[0]);
cout << "Array size: " << size << endl;

// Iterating through the array


for (int i = 0; i < size; ++i) {
cout << "Element " << i + 1 << ": " << numbers[i] << endl;
}

return 0;
}
5. Multi-dimensional Arrays:
A multi-dimensional array is an array of arrays, meaning it's a data structure that organizes elements in
multiple dimensions.

#include <iostream>
using namespace std;

int main() {
// Declaration and Initialization of a 2D array
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Accessing elements of a 2D array


for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << "Element at position (" << i << ", " << j << "): " << matrix[i][j] << endl;
}
}

return 0;

Sorting
Sorting algorithms are used to rearrange the elements of a collection in a specific order. There are
various sorting algorithms, each with its own advantages and disadvantages. Here, I'll explain a few
common sorting algorithms:

1. Bubble Sort:
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the
list is sorted.

#include <iostream>
using namespace std;

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5; // Manually specify the size of the array
// Bubble Sort
for (int i = 0; i < n-1; ++i) {
for (int j = 0; j < n-i-1; ++j) {
if (arr[j] > arr[j+1]) {
// swap elements if they are in the wrong order
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

// Display the sorted array


cout << "Sorted array: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}

return 0;
}

2. Selection Sort:
Selection Sort sorts an array by repeatedly finding the minimum element from the unsorted part of the
array and putting it at the beginning.

#include <iostream>
using namespace std;

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5; // Manually specify the size of the array

// Selection Sort
for (int i = 0; i < n-1; ++i) {
int minIndex = i;
for (int j = i+1; j < n; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// swap the found minimum element with the first element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
// Display the sorted array
cout << "Sorted array: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}

return 0;
}

3. Insertion Sort: [Not Important]


Insertion Sort builds the final sorted array one item at a time by repeatedly taking an element from the
unsorted part and inserting it into its correct position in the sorted part.

#include <iostream>
using namespace std;

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5; // Manually specify the size of the array

// Insertion Sort
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;

// Move elements greater than key to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}

// Place the key in its correct position


arr[j + 1] = key;
}

// Display the sorted array


cout << "Sorted array: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}

return 0;
}

You might also like