Creative Commons License Foxbond's Repo

/** (c) 2012 Michał (Foxbond) Chraniuk */
#include <conio.h> //_getch
#include <iostream> //cin, cout
#include <string> //string
#include <sstream>  //istringstream
#include <iterator> //istream_iterator
#include <vector>   //vector

using namespace std;

void cls () { system("cls"); }

void pause () { _getch(); }

vector<string> explode(string const & s, char delim)
{
    vector<string> result;
    istringstream iss(s);

    for (string token; getline(iss, token, delim); )
    {
        if (!token.empty()) result.push_back(move(token));
    }

    return result;
}


int main (){

	string source;
	int iloscWykonan = 0;

	cout << "Podaj liczby rozdzielajac przecinkiem: ";
	cin >> source;

    vector<string> v = explode(source, ',');

	int n = v.size();
	int* tab=new int[n];

	for ( int ii = 0; ii < n; ii++){
		istringstream iss(v[ii]);
		iss >> tab[ii];
	}

	int i = 1;
	
	int j, pom;
	////i := 2; n := Length(a);
	while (i < n) {
		// a[1] ≤ ... ≤ a[i-1], i ≤ n+1 oraz i ≤ n
		j = i; 
		pom = tab[i];
		// a[i] = pom
		while (j>0 && tab[(j-1)]>pom) {
			// pom ≤ a[j+1] ≤...≤ a[i] oraz a[j-1]>pom
			tab[j] = tab[(j-1)];
			// pom ≤ a[j] ≤ a[j+1] ≤...≤ a[i]
			j = j - 1;

			iloscWykonan++;
		}
		// a[1] ≤...≤ a[j-1] ≤ pom ≤ a[j+1] ≤...≤ a[i]
		tab[j] = pom;
		// a[1] ≤...≤ a[i-1] ≤ a[i]
		i = i+1;
		// a[1] ≤… ≤ a[i-2] ≤ a[i-1], i ≤ n+1
	}
	// a[1] ≤... ≤ a[i-2] ≤ a[i-1] oraz i=n+1
	

	cout << tab[0];
	for (i=1;i<n;i++){
		cout << ", " << tab[i] ;
	}

	cout << "\nLiczba wykonan wewnetrznej petli: " << iloscWykonan << "\n";


	pause();
	return 0;
}

> Back