Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab MIPS

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <iostream>

#include <time.h>
using namespace std;
#define size 1e5
double data[15][(int)size];
void output(float A[], int n) {
for (int i = 0; i < n; i++)
cout << A[i] <<" ";
}
void autocre(float A[], int n) {
for (int i = 0; i < n; i++) {
A[i] = rand()+ (float)rand() / RAND_MAX;
}
}
void cre(float A[], int n) {
for (int i = 0; i < n; i++) {
cin >> A[i];
}
}
void limitedcre(float A[], int n) {

for (int i = 0; i < n; i++) {


A[i] = 39000 + 27233 + rand() + (float)rand() / RAND_MAX;
}
}
void selectionSort(float arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
void insertionSort(float arr[], int length) {
int j;
float temp;

for (int i = 0; i < length; i++) {


j = i;

while (j > 0 && arr[j] < arr[j - 1]) {


temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
}
float timesorting(float A[], int n) {
clock_t start, end;
float cpu_time_used;
start = clock();
selectionSort(A, n);
end = clock();
cpu_time_used = ((float)(end - start)) / CLOCKS_PER_SEC;
return cpu_time_used;
}
void input2D(double a[][(int)size], int row, int col){
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
a[i][j] = rand() + (float)rand() / RAND_MAX;
}
}
}
void result(double a[][(int)size], int row, int col) {

int main()
{
int n;
cin >> n;
float* A = new float[n];
int luachon;

while (1)
{
cout << endl;
cout << "---------- MENU-----------" << endl;
cout << " 1. Creat ArrayX." << endl;
cout << " 2. Creat randArrayX." << endl;
cout << " 3.RandArrayX[39000:99000]" << endl;
cout << " 4. Output mang." << endl;
cout << " 5.Sapxep tang." << endl;
cout << " 6.Tinh thoi gian sort..." << endl;

cin >> luachon;


switch (luachon)
{
case 1: cin >> n;
cre(A, n); break;
case 2:
autocre(A, n); break;
case 3: limitedcre(A, n); break;
case 4: output(A, n); break;
case 5: selectionSort(A, n); break;
case 6:cout<<timesorting(A, n); break;
}
}
}

You might also like