Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 24.01.2012, 21:59   #1
Sobaka_ru
Пользователь
 
Регистрация: 09.12.2010
Сообщений: 44
По умолчанию Сортировка массивом

Какой метод сортировки тут используется????
Можно ли как нибудь подстроить под метод подсчета???
Код:
#include "stdafx.h"
#include <conio.h>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;

const int N = 5;
struct Racer
{
 char name[40];
 int championship;
 int victories;
 int pole_pozition;
};
typedef Racer *pRacer;

//============================================
// Filling an array of structures.
Racer filling_struct(Racer ob[], const int N)
{
 strcpy(ob[0].name, "Fernando Alonso");
 ob[0].championship = 2;
 ob[0].victories = 32;
 ob[0].pole_pozition = 26;

 strcpy(ob[1].name, "Michael Shumacher");
 ob[1].championship = 7;
 ob[1].victories = 91;
 ob[1].pole_pozition = 67;

 strcpy(ob[2].name, "Lewis Hamilton");
 ob[2].championship = 1;
 ob[2].victories = 26;
 ob[2].pole_pozition = 19;

 strcpy(ob[3].name, "Felipe Massa");
 ob[3].championship = 0;
 ob[3].victories = 18;
 ob[3].pole_pozition = 15;

 strcpy(ob[4].name, "Sebastjan Vettel");
 ob[4].championship = 2;
 ob[4].victories = 28;
 ob[4].pole_pozition = 27;

 return *ob;
}
//============================================
// QuickSort.
void QuickSort(Racer ob[], int left, int right)
{
 pRacer p[5];
 for(int i=0; i<N; i++)
  p[i] = &ob[i];

 int i, j;
 int x;

 i = left; j = right;
 x = p[(left+right)/2]->victories;

 do
 {
    while((p[i]->victories < x) && (i < right)) i++;
	while((x < p[j]->victories) && (j > left)) j--;

	if(i <= j)
	{
       swap(p[i], p[j]);
	   i++; j--;
	}
 }
 while(i <= j);

 if(left < j) QuickSort(ob, left, j);
 if(i < right) QuickSort(ob, i, right);
 
 FILE *fout;
 fout = fopen("C:\\Users\\1\\Documents\\Visual Studio 2008\\Projects\\0001)\\MStruct_in_File(QuickSorting)\\Debug\\output.txt", "w");
 for(int k1=i; k1<N; k1++)
  fprintf(fout, "%20s   %6d   %6d   %6d\n", p[k1]->name, p[k1]->championship, p[k1]->victories, p[k1]->pole_pozition);
 for(int k2=i-1; k2>=0; k2--)
  fprintf(fout, "%20s   %6d   %6d   %6d\n", p[k2]->name, p[k2]->championship, p[k2]->victories, p[k2]->pole_pozition);
 fclose(fout);
}
//============================================
// Record in the file.
void Record(Racer ob[], const int N)
{
 FILE *fin;
 fin = fopen("C:\\Users\\1\\Documents\\Visual Studio 2008\\Projects\\0001)\\MStruct_in_File(QuickSorting)\\Debug\\input.txt", "w");
 for(int i=0; i<N; i++)
  fprintf(fin, "%20s   %6d   %6d   %6d\n", ob[i].name, ob[i].championship, ob[i].victories, ob[i].pole_pozition);
 fclose(fin);
}
//============================================

int main()
{
 Racer R[N];
 *R = filling_struct(R, N);

 Record(R, N);

 QuickSort(R, 0, N-1);
  //_getch();	
 return 0;
}
Sobaka_ru вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Сортировка Шелла и Шейкер-сортировка AleksandrMakarov Паскаль, Turbo Pascal, PascalABC.NET 11 11.03.2012 12:18
Быстрая сортировка(сортировка хаора) с++ LustHunter Помощь студентам 3 07.10.2011 19:37
Сортировка массива методами предсортировки и слияния, и пирамидальная сортировка. lenny_24 Помощь студентам 2 17.04.2011 18:57
паскаль,одномерный массив,сортировка вставка,сортировка убывания,от максимального до конца немозг Помощь студентам 11 06.02.2010 21:57
Сортировка файлов в Explorer vs сортировка в Delphi mutabor Общие вопросы Delphi 11 04.09.2009 14:32