Skip to content

Instantly share code, notes, and snippets.

@libbkmz
Created June 1, 2013 15:28
Show Gist options
  • Select an option

  • Save libbkmz/5690746 to your computer and use it in GitHub Desktop.

Select an option

Save libbkmz/5690746 to your computer and use it in GitHub Desktop.
/*
* File: main.cpp
* Author: bkmz
*
* Created on 27 Октябрь 2009 г., 19:39
*/
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <functional>
#include <sys/time.h>
using namespace std;
#define WANT_CREATE_SORT_NEW 1 //Если хотим сгенерировать новый файл с матрицей ставим 1 если нет 0
#define SHOW_MATRIX 1 //Если хотим видеть матрицы в изначальном и отсортированном виде ставим 1 не хотим видеть ничего ставим 0
#define SHOW_DEBUG_TIME 0 //Если хотим видеть время исполнения нашей программы
#define OUTPUT_INTO_FILE_SORTED 0 //Для вывода отсортированной матрицы в файл - пока НЕ РЕАЛИЗОВАНО!!!!!!!
#define USING_BKMZ_SORTING 0 //Исползьовать мою сортировку пузырьком вместо stl
vector <vector <int> > matrix; //Создаем двумерный массив
int tmp,n,m;
clock_t time1,time2,time3;
void out_mas (int n,int m){ //Функция генерации матрицы и запись её в файл
srand( (unsigned)time( NULL ) );
ofstream output("outmat.txt");
output << n << " " << m << endl;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
output << rand() % 10 << " " ;
}
output << endl;
}}
void coutput_matrix(int n,int m){ //Функция вывода матрицы на экран
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout << matrix[i][j] << "\t";
}
cout << endl;
}}
void write_sorted (vector< vector<int> > matrix, int n, int m){
ofstream output("out_sorted.txt");
output << n << " " << m << endl;
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
output << matrix[i][j] << "\t";
}
output << endl;
}
}
vector<int> buble_sort_bkmz(vector<int> massiv, int ne ){
for(int i=0; i < ne; i++) { // i - номер прохода
for(int j = ne-1; j > i; j-- ) { // внутренний цикл прохода
if ( massiv[j-1] > massiv[j] ) {
swap(massiv[j],massiv[j-1]);
}
}
}
return massiv;
}
int main(){
time1 = clock();
#if WANT_CREATE_SORT_NEW == 1
out_mas (4,5); //Вызываем функцию, для того чтобы она нам сгенерила новую матрицу
#if SHOW_DEBUG_TIME == 1
cout << "Generating matrix was " << clock() - time1 << endl;
#endif
#endif
time2 = clock();
ifstream input("outmat.txt");
input >> n >> m;
for (int i=0;i<n;i++){ //Заносим данные из файла в массив matrix
matrix.push_back(vector<int>());
for(int j=0;j<m;j++){
matrix[i].push_back(0); // добавляем пустой элемент в текущую строку
input >> tmp;
matrix[i][j] = tmp; // заполняем этот элемент
}
}
#if SHOW_DEBUG_TIME == 1
cout << "Inserting matrix into memery " << clock() - time2 << endl;
cout << "\tAll time working of programm " << clock() - time1 << endl;
#endif
#if SHOW_MATRIX == 1
coutput_matrix(n,m); cout << endl << endl << endl << endl << endl << endl;
#endif
time2 = clock();
for (int i=0;i<n;i++){
#if SHOW_DEBUG_TIME == 1
cout << i << " Index sorting...";
time3 = clock();
#endif
#if USING_BKMZ_SORTING == 1
matrix[i] = buble_sort_bkmz(matrix[i],m);
#if SHOW_DEBUG_TIME == 1
cout << "\t" << clock() - time3 << endl;;
#endif
#else
sort(matrix[i].begin(), matrix[i].end()); //сортируем в не убывающем порядке
#if SHOW_DEBUG_TIME == 1
cout << "\t" << clock() - time3 << endl;;
#endif
#endif
// sort(matrix[i].begin(), matrix[i].end(), greater<int>( )); //сортируем в убывающем порядке
}
#if SHOW_DEBUG_TIME == 1
cout << "Sorting the matrix " << clock() - time2 << endl;
cout << "\tAll time working of programm " << clock() - time1 << endl;
#endif
#if SHOW_MATRIX == 1
coutput_matrix(n,m);
#endif
#if OUTPUT_INTO_FILE_SORTED == 1
time2 = clock();
write_sorted(matrix, n, m);
#endif
#if SHOW_DEBUG_TIME == 1
#if OUTPUT_INTO_FILE_SORTED == 1
cout << "Output matrix info a file " << clock() - time2 << endl;;
#endif
cout << "Bkmz Matrix Sorting was workin " << clock() - time1;
#endif
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment