Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Thursday, April 23, 2020

Arrays and basic functions associated with Array in JavaScript

About Arrays in JavaScript:

An array is a list of elements which is to store a large amount of element under a single name. The length of the array in JavaScript is not fixed.

=> Let's define an array with numbers stored form 1 to 5.

let number = [1, 2, 3, 4, 5];

=> You can define an array with string elements.

let animal = ["dog", "Cat", "monkey", "tiger"];

=> We can also define an array with mixed elements.

let data  = [1, 2, "hello", 4];

How to access any index element in JavaScript:
For example, we want to access the second element of the data array;
we will use 

data[1]; 

because of indexing in javascript array start from 0.


Now let's perform some operation on arrays by using build-in methods;

1. Length of the array:

' let numbers = [1, 2, 3, 4];
numbers.length; 
// 4





2. Map function to loop over the array and perform an action on each element in this case double all element and store it in the new array:

let numbers = [1, 2, 3, 4];

let doubleNumbers =  numbers.map(function(num) {


    return 2 * num;

});

console.log(doubleNumbers);

// [2. 4. 6. 8]

So, in the above map function, we are performing the anonymous function which is doubling all the element and returning it to doubleNumbers.


3. Adding a new element at the end of the array (push function):

let numbers = [1, 2. 3. 4];/div>


numbers.push(5);

console.log(numbers);

// [1, 2, 3, 4, 5];


4. Remove element from the end of Array (pop function):

let numbers = [1, 2, 3, 4];

numbers.pop();

console.log(numbers);

// [1, 2, 3]
















Wednesday, September 26, 2018

Program of Quick Sort in C++


#include<iostream>
using namespace std;

void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

int partition(int arr[], int start, int end)
{
int pivot = arr[end];
int i,j;

j = start;
for (i = start; i < end; i++)
{
if (arr[i] <= pivot)
{
swap(&arr[i], &arr[j]);
j++;
}

}
swap(&arr[j], &arr[end]);
return j;
}

void quicksort(int arr[], int start, int end)
{
int part;

if (start < end)
{
part = partition(arr, start, end);

quicksort(arr, start, part - 1);
quicksort(arr, part + 1, end);
}
}

int main()
{
int size;
cout<<"\n Enter the size of the array : ";
cin>>size;

int arr[size];

cout<<"\n Enter "<<size<<" elements in the array : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}

quicksort(arr, 0, size - 1);

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

return 0;
}


Monday, September 24, 2018

Selection Sort Program in C++

//Selection Sort Program
#include<iostream>
using namespace std;

#include<conio.h>

void SelectionSort(int arr[], int size)
{
int min,pos;
for (int i = 0; i < size - 1; i++)
{
min = arr[i];
pos = i;
//Finding the smallest element
for (int j = i + 1; j < size; j++)
{
if (arr[j] < min)
{
min = arr[j];
pos = j;
}
}
//Swaping the smallest element to its accurate position
arr[pos] = arr[i];
arr[i] = min;
}
}

int main()
{
int size;

cout<<"\n Enter the size of the array : ";
cin>>size;

int arr[size];

cout<<"\n Enter the Elements in the array : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}

SelectionSort(arr, size);

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

getch();
return 0;
}


Program of Bubble Sort in C++

//Bubble Sort Program
#include<iostream>
using namespace std;
#include<conio.h>

//Function of Bubble Sort
void BubbleSort(int arr[], int size)
{
int temp;

for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main()
{
int size;

cout<<"\n Enter the size of the array : ";
cin>>size;

int arr[size];

cout<<"\n Enter the elements to be sorted : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}

BubbleSort(arr, size);

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

getch();
return 0;
}


Wednesday, September 19, 2018

Merge Sort Program in C++

//MERGE SORT PROGRAM IN C++

#include<iostream>
using namespace std;

void merge(int arr[], int start, int size, int mid)
{
int s1, s2, i, j;

//Assigning size to the two array to be merged.
s1 = mid - start + 1;
s2 = size - mid;

int arr1[s1], arr2[s2];

//Initailizing those array
for (i = 0; i < s1; i++)
arr1[i] = arr[start + i];
for (j = 0; j < s2; j++)
arr2[j] = arr[mid + 1 + j];
i = 0;
j = 0;

int k = start;

//Merging them in sorted array
while (i < s1 && j < s2)
{
if (arr1[i] <= arr2[j])
{
arr[k] = arr1[i];
i++;
}
else
{
arr[k] = arr2[j];
j++;
}
k++;
}

while (i < s1)
{
arr[k] = arr1[i];
i++;
k++;
}

while (j < s2)
{
arr[k] = arr2[j];
j++;
k++;
}
}

void mergesort(int arr[],int start, int size)
{
int mid;

if (size - start + 1 < 2)
{
return;
}

else
{
mid = (start + size)/2;
//Sorting left array
mergesort(arr, start, mid);
//Sorting right array
mergesort(arr, mid + 1,size);
merge(arr, start, size, mid);
}
}

int main()
{
int arr[10], size;
cout<<"\n Enter the size of array : ";
cin>>size;
cout<<"\n Enter the element in the array : ";
for (int i = 0; i < size; i++)
cin>>arr[i];

mergesort(arr, 0, size-1);
cout<<"\n Sorted Array is : ";
for (int i = 0; i < size; i++)
cout<<arr[i]<<" ";
return 0;
}