+1 678 648 4277 
Table Of Contents
  • Sorting Arrays and Checking prime Numbers

Sorting Arrays and Checking prime Numbers

Create a program that sorts an array of integers in ascending order using selection, insertion or merge sorting algorithm. The program should indicate whether each integer element is a prime number. Also, Make sure that the program is decomposed on into appropriate functions which could be reused on other data structures.
 C++ Sorting Program
/*
 * sort.cpp
 *
* Created on: Mar 2, 2021
 * Author: thanh
 */
#include
#include
using namespace std;
#define DEFAULT_LENGTH 25
void swap(int *xp, int *yp) {
int temp = *xp;
  *xp = *yp;
  *yp = temp;
}
voidselectionSort(int *arr, int n) {
inti, 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]
min_idx = j;
    // Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
  }
}
boolisPrimeNumber(int number) {
if (number == 0 || number == 1)
return false;
for (inti = 2; i<= number / 2; ++i) {
if (number % i == 0) {
return false;
    }
  }
return true;
}
voidprintArray(int *array, int length) {
  // Print element of array:
for (inti = 0; i< length; i++) {
cout<< array[i] << " ";
  }
cout<
}
int main(intargc, char **argv) {
int length = 0;
int *array = NULL;
cout<< "=================================" <
cout<< "= SORTING =" <
cout<< "=================================" <
  // Ask length until this is valid
while (length < DEFAULT_LENGTH) {
cout<< "Enter length of array: ";
cin>> length;
if (length < DEFAULT_LENGTH) {
cout
<< "The length should be greater than zero and less than or equal to twenty-five. Please try again."
<
continue;
    }
break;
  }
  // Dynamic allocated array
array = new int[length];
  // random assign data
for (inti = 0; i< length; i++) {
    // Value between 1 and 100
array[i] = rand() % 100 + 1;
  }
  // Print array
cout<< "Array: ";
printArray(array, length);
  // Create a copy array
int *copyArray = new int[length];
  // Copy array
for (inti = 0; i< length; i++) {
copyArray[i] = array[i];
  }
  // Sort array using selectionSort
selectionSort(copyArray, length);
  // Print array
cout<< "Array after sort: ";
printArray(copyArray, length);
  // Just print the Prime number
cout<< "Prime number: ";
for (inti = 0; i< length; i++) {
if (isPrimeNumber(copyArray[i])) {
cout<
    }
  }
cout<
}
Output Image