/*
* MySelectSort.h
* Created on: 2009-12-21
* Programming Language: C++
* Operating system: Window XP
* Environment: DEV-C++ and Eclipse
* Author: http://blog.youkuaiyun.com/programs
*/
* MySelectSort.h
* Created on: 2009-12-21
* Programming Language: C++
* Operating system: Window XP
* Environment: DEV-C++ and Eclipse
* Author: http://blog.youkuaiyun.com/programs
*/
#ifndef MYSELECTSORT_H_
#define MYSELECTSORT_H_
#define MYSELECTSORT_H_
#define DEBUG
typedef double DataType;
class SimpleArray
{
public:
SimpleArray(int size);
~SimpleArray();
void InsertItem(DataType value);
void MySelectSort();
void MyDisplay();
void MySwap(DataType &x, DataType &y);
int GetSwapTimes();
int GetCompareTimes();
private:
DataType *ptr;
int size;
#if defined (DEBUG)
int swap_times;
int compare_times;
#endif /* DEBUG */
};
{
public:
SimpleArray(int size);
~SimpleArray();
void InsertItem(DataType value);
void MySelectSort();
void MyDisplay();
void MySwap(DataType &x, DataType &y);
int GetSwapTimes();
int GetCompareTimes();
private:
DataType *ptr;
int size;
#if defined (DEBUG)
int swap_times;
int compare_times;
#endif /* DEBUG */
};
#endif /* MYSELECTSORT_H_ */
/* ***************************************** */
/*
* MySelectSort.cpp
* Created on: 2009-12-21
* Programming Language: C++
* Operating system: Window XP
* Environment: DEV-C++ and Eclipse
* Author: http://blog.youkuaiyun.com/programs
*/
* MySelectSort.cpp
* Created on: 2009-12-21
* Programming Language: C++
* Operating system: Window XP
* Environment: DEV-C++ and Eclipse
* Author: http://blog.youkuaiyun.com/programs
*/
#include "MySelectSort.h"
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
SimpleArray::SimpleArray(int size)
{
ptr = new DataType[size];
this->size = 0;
#if defined (DEBUG)
this->swap_times = 0;
this->compare_times = 0;
#endif /* DEBUG */
}
{
ptr = new DataType[size];
this->size = 0;
#if defined (DEBUG)
this->swap_times = 0;
this->compare_times = 0;
#endif /* DEBUG */
}
SimpleArray::~SimpleArray()
{
delete ptr;
}
{
delete ptr;
}
void SimpleArray::InsertItem(DataType value)
{
ptr[size] = value;
++size;
}
{
ptr[size] = value;
++size;
}
void SimpleArray::MySelectSort()
{
int i, j, min_index;
for (i = 0; i < (this->size); ++i)
{
min_index = i;
for (j = i + 1; j < (this->size); ++j)
{
if (ptr[j] < ptr[min_index])
{
min_index = j;
}
#if defined (DEBUG)
++compare_times;
#endif /* DEBUG */
}
MySwap(ptr[i], ptr[min_index]);
}
}
{
int i, j, min_index;
for (i = 0; i < (this->size); ++i)
{
min_index = i;
for (j = i + 1; j < (this->size); ++j)
{
if (ptr[j] < ptr[min_index])
{
min_index = j;
}
#if defined (DEBUG)
++compare_times;
#endif /* DEBUG */
}
MySwap(ptr[i], ptr[min_index]);
}
}
void SimpleArray::MySwap(DataType &x, DataType &y)
{
DataType temp;
temp = x;
x = y;
y = temp;
}
{
DataType temp;
temp = x;
x = y;
y = temp;
}
void SimpleArray::MyDisplay()
{
int i = 0;
for (i = 0; i < (this->size); ++i)
{
cout << ptr[i] << "\t";
}
cout << endl;
#if defined (DEBUG)
++swap_times;
#endif /* DEBUG */
}
{
int i = 0;
for (i = 0; i < (this->size); ++i)
{
cout << ptr[i] << "\t";
}
cout << endl;
#if defined (DEBUG)
++swap_times;
#endif /* DEBUG */
}
#if defined (DEBUG)
int SimpleArray::GetCompareTimes()
{
return compare_times;
}
int SimpleArray::GetCompareTimes()
{
return compare_times;
}
int SimpleArray::GetSwapTimes()
{
return swap_times;
}
#endif /* DEBUG */
{
return swap_times;
}
#endif /* DEBUG */
/* ***************************************** */
/*
* TestMain.cpp
* Created on: 2009-12-21
* Programming Language: C++
* Operating system: Window XP
* Environment: DEV-C++ and Eclipse
* Author: http://blog.youkuaiyun.com/programs
*/
* TestMain.cpp
* Created on: 2009-12-21
* Programming Language: C++
* Operating system: Window XP
* Environment: DEV-C++ and Eclipse
* Author: http://blog.youkuaiyun.com/programs
*/
#include "MySelectSort.h"
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main(void)
{
SimpleArray arr(10);
{
SimpleArray arr(10);
int i = 0;
for (i = 10; i > 0; --i)
{
arr.InsertItem(i);
}
for (i = 10; i > 0; --i)
{
arr.InsertItem(i);
}
cout << "Before sorted: " << endl;
arr.MyDisplay();
arr.MyDisplay();
cout << "Being sorted..." << endl;
arr.MySelectSort();
arr.MySelectSort();
cout << "After sorted: " << endl;
arr.MyDisplay();
arr.MyDisplay();
return 0;
}
}
转载于:https://blog.51cto.com/programs/247310