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

112 學年度第 1 學期

EE2120 資料結構
Programming Exercise?
簡易報告
甲班

2023/09/16
一、 目的
請說明此程式練習之目的。
(字體:新細明體,大小:14,單行間距,左右對齊)

運用亂數出來的數字進行比較大小的排序並插入數字,再來就是
使用 performance analysis 並列出 worst case & best case,最後使用
clock()來計算最壞和最好情況下程序的執行時間。
二、 程式碼與執行結果
請列出最後版本之程式碼,並將執行結果之截圖貼附於本節中。
(字體:新細明體,大小:14,單行間距,左右對齊)
/
***********************************************************
***
EE2120 Data Structure
Programming Exercise #2 working template

Goals:
I. Programming implementation of Insertion Sort Algorithm
II. Performance Analysis and Measurements for the program

Copyright (C) 2023

Wen-Yen Lin
wylin@mail.cgu.edu.tw
Department of Elecetrical Engineering
Chang Gung University
***********************************************************
****/

// These are the header files that you will need in this exercise
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SIZE 1001


int moveplacement_count;
int step_count;

void RandomNumberGenerator(int a[], int n)


{
int i;

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


a[i] = rand();
}

// Used in Part I
// & Part II for performance measurement
void InsertionSort(int a[], int n)
{
//For you to do: Implement the algorithm of Insertion Sort here.
//a[] is the initial input array with number of n integers to be
//sorted out in ascending order. The sorted array is still put in
//a[].
int i;

for (i = 1; i < n; i++)


{
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}

// Used in Part II performance analysis


void InsertionSort_PerformanceAnalysis(int a[], int n)
{
//For you to do: Implement the algorithm of Insertion Sort with
//movement&placement counts and steps counts here.
//Use the global declared variables, "moveplacement_count" for
movement and placement count
//and "step_count" for step count.
//a[] is the initial input array with number of n integers to be
//sorted out in ascending order. The sorted array is still put in
//a[].
int i, t, j;
moveplacement_count = 0;
step_count = 0;
for (i = 1; i < n; i++)
{
step_count++;
t = a[i];
step_count++;
for (j = i - 1; j >= 0 && t < a[j]; j--)
{
step_count++;
a[j + 1] = a[j];
moveplacement_count++;
step_count++;
}
step_count++;
a[j + 1] = t;
moveplacement_count++;
step_count++;
}
step_count++;
}

void main()
{
int i, n, counter;
int a[MAX_SIZE];
clock_t start;
double initializeTime, elaspedTime;

//Part I: Implement the Insertion Sort algorithm in C. Use


RandomNumberGenerator
//provided to generate a sequene of N positive number and test with
your program.

//Initialize the seed for random number generator


srand((unsigned)time(NULL));
//srand(clock());
printf("Programming Exercise #2 for the course of Data Structure
@2021\n");

//Ask for the number of elements in the sequence


printf("Please enter the number of random numbers to be generated:
");
scanf_s("%d", &n);

//Call RandomNumberGenerator to randomly generate a sequence of


N positive numbers
RandomNumberGenerator(a, n);

//The following is for you to visually verify if your insertion sort


//algorithm works fine or not.
//Print out the sequence
printf("The input sequence is:");
for (i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");

//Call to your InsertionSort function


InsertionSort(a, n);

//Print out the sequence after sorting


printf("The sorted output sequence is:");
for (i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");

printf("Please verify if your output sequence is in ascending order...\


n<PRESS ANY KEY TO CONTINUE>\n");
_getch();

//Part II: Performance Analysis and Measurement


//Performance Analysis

//Implementing "Movement-and-placement Counts" and "Step


Counts" into
//Insertion Sort Algorithm by completing the
InsertionSort_PerformanceAnalysis() function
//and find out the movement-and-placement counts and step counts
//for worst cases and best cases. Repeat for N = 10, 20, 30, ..., 100,
200, ..., 1000
//Plot the data v.s. N

//Note: You have to understand what would be the worst case and
best case and initialize
//your input sequence accordingly.

printf("============= Movement-and-Placement Count Worst


Case Test ==============\n");
n = 10;
while (n <= 1000) {
//For you to do. You have initialize the a[] into worst case for
insertion sort
//...............
for (i = 0; i < n; i++)
a[i] = n - i;

//Then call InsertionSort_PerformanceAnalysis


//...............
InsertionSort_PerformanceAnalysis(a, n);

//print out the movement-and-placement and step counts result


for each N.
printf("n=%d => move&placement_count=%d, step_count=%d\
n", n, moveplacement_count, step_count);

// continue next test instance.


if (n < 100) n += 10;
else n += 100;
}

printf("============= Movement-and-Placement Count Best


Case Test ==============\n");
n = 10;
while (n <= 1000) {
//For you to do. You have initialize the a[] into worst case for
insertion sort
//...............
for (i = 0; i < n; i++)
a[i] = i + 1;

//Then call InsertionSort_PerformanceAnalysis


//...............
InsertionSort_PerformanceAnalysis(a, n);

//print out the movement-and-placement and step counts result


for each N.
printf("n=%d => move&placement_count=%d, step_count=%d\
n", n, moveplacement_count, step_count);

// continue next test instance.


if (n < 100) n += 10; // n = 10, 20, 30, ...., 90
else n += 100; // n = 100, 200, 300, ..., 1000
}

//Performance Measurement
//Mesaurement the execution time of the Insertion Sort Algorithm
//using clock() as we introduced in the class. Find out the duration of
worst case and
//best case for n = 10, 20, 30, ..., 100, 200, ..., 1000. Plot your results
v.s. n
printf("CLOCKS_PER_SEC=%d\n", CLOCKS_PER_SEC);
printf("=========== Performance Measurement Worst Case Test
============\n");
n = 10;
while (n <= 1000) {
// to measure the time needed for initializing the test case into
worst case first.
counter = 0;
start = clock();
do {
counter++;
//For you to do. The way to initialize the a[] into worst case
//................
for (i = 0; i < n; i++)
a[i] = n - i;

} while ((clock() - start) < CLOCKS_PER_SEC);


initializeTime = (double)(clock() - start) / CLOCKS_PER_SEC;
initializeTime = initializeTime / counter;

// then measure the execution time for initialization and the


insertion algorithm
counter = 0;
start = clock();
do {
counter++;
//For you to do. The way to initialize the a[] into worst case
//...............
for (i = 0; i < n; i++)
a[i] = n - i;

//Then call InsertionSort


//...............
InsertionSort(a, n);

} while ((clock() - start) < CLOCKS_PER_SEC);


elaspedTime = (double)(clock() - start) / CLOCKS_PER_SEC;
elaspedTime = elaspedTime / counter - initializeTime;
printf("n=%d => elaspedTime=%e\n", n, elaspedTime);

// continue next test instance.


if (n < 100) n += 10; // n = 10, 20, 30, ...., 90
else n += 100; // n = 100, 200, 300, ..., 1000
}

printf("=========== Performance Measurement Best Case Test


============\n");
n = 10;
while (n <= 1000) {
// to measure the time needed for initializing the test case into
worst case first.
counter = 0;
start = clock();
do {
counter++;
//For you to do. The way to initialize the a[] into worst case
//................
for (i = 0; i < n; i++)
a[i] = i + 1;

} while ((clock() - start) < CLOCKS_PER_SEC);


initializeTime = (double)(clock() - start) / CLOCKS_PER_SEC;
initializeTime = initializeTime / counter;

// then measure the execution time for initialization and the


insertion algorithm
counter = 0;
start = clock();
do {
counter++;
//For you to do. The way to initialize the a[] into worst case
//...............
for (i = 0; i < n; i++)
a[i] = i + 1;

//Then call InsertionSort


//...............
InsertionSort(a, n);

} while ((clock() - start) < CLOCKS_PER_SEC);


elaspedTime = (double)(clock() - start) / CLOCKS_PER_SEC;
elaspedTime = elaspedTime / counter - initializeTime;
printf("n=%d => elaspedTime=%e\n", n, elaspedTime);

// continue next test instance.


if (n < 100) n += 10; // n = 10, 20, 30, ...., 90
else n += 100; // n = 100, 200, 300, ..., 1000
}

//Write down the operation counts and step counts as well as the
execution time,
//then, use the Excel template file provided for the plot

}
三、 所遇到之問題與解決方式
請列出 3 項於本程式練習中,所碰到之程式撰寫錯誤或是警告,
並說明解決方法。
(字體:新細明體,大小:14,單行間距,左右對齊)
 問題一:
 錯誤或警告:連結器工具錯誤 LNK2019
 解決方法:檢查後重新將 InsertionSort 輸入好
 問題二:
 錯誤或警告:連結器工具錯誤 LNK1120
 解決方法:同上檢查後重新將 InsertionSort 輸入好
四、 討論與心得
請討論於練習中之學習心得,若此程式練習有題目需要回答,
請於本章節中回答。
(字體:新細明體,大小:14,單行間距,左右對齊)
距離之前使用 Visual Studio 大概是一年多前,確實忘了蠻多東
西的,也希望老師語速能放緩一些讓我跟同學能吸收得進去。

You might also like