Important alert: (current site time 7/15/2013 5:55:21 PM EDT)
 

VB icon

base sortings

Email
Submitted on: 1/2/2007 4:51:07 PM
By: Balogh  
Level: Advanced
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++, UNIX C++
Views: 6915
author picture
(About the author)
 
     sorting an array
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :base sortings
//**************************************
#include <iostream>
using namespace std;
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: base sortings
// Description:sorting an array
// By: Balogh
//
// Inputs:an array
//
// Assumes:in zip:
http://bhsite.extra.hu/data/rendezes_source.zip
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=11113&lngWId=3//for details.//**************************************

/*
	author: bh
	e-mail: baloghbh@gmail.com
	site: http://bhsite.extra.hu
*/
#include <iostream>
using namespace std;
	// headers
//---------------------------------------------
unsigned short *array = NULL;
	// own array, vector
short i = 0, j = 0;
	// helping variables 
unsigned short *temp = NULL;
	// temporary variable
//---------------------------------------------
void InitMemArray(unsigned short piece)
{
	array = new unsigned short[piece];
		// memory use
}
//---------------------------------------------
void DelMemArray(unsigned short piece)
{
	delete[piece] array;
		// memory freeing
}
//---------------------------------------------
void Swap(unsigned short piece)
{
	for (i = 0; i < piece-1; i++)
	{
		for (j = 0; j < piece; j++)
		{
			cout << " " << array[j] << " ";
		}
		cout << endl << endl;
			// print the array
		for (j = i + 1; j < piece; j++)
		{
			if (array[i] > array[j])
			{
				temp = new unsigned short;
				*temp = array[i];
				array[i] = array[j];
				array[j] = *temp;
					// swap
				delete temp;
			}
		}
	}
	for (j = 0; j < piece; j++)
	{
			cout << " " << array[j] << " ";
	}
	cout << endl << endl;
		// print the array
}
//---------------------------------------------
void Insertion(unsigned short piece)
{
	for (i = 1 ;i < piece ;i++)
	{
		temp = new unsigned short;
		*temp = array[i];
		j = i-1;
		while (j >= 0 && *temp < array[j])
		{
			array[j+1] = array[j];
			j = j-1;
		}
		array[j+1] = *temp;
		delete temp;
		for (j = 0; j < piece; j++)
		{
			cout << " " << array[j] << " ";
		}
		cout << endl << endl;
	}
}
//---------------------------------------------
void Bubble_Sorting(unsigned short piece)
{
	for (i = 1; i < piece; i++)
	{
		for (j = i - 1; j >= 0 && array[j] > array[j+1]; j--)
		{
			temp = new unsigned short;
			*temp= array[j];
			array[j] = array[j+1];
			array[j+1] = *temp;
			delete temp;
		}
		for (int k = 0; k < piece; k++)
		{
			cout << " " << array[k] << " ";
		}
		cout << endl << endl;
	}
}
//---------------------------------------------
void Shell(unsigned short piece)
{
	int k;
	for (k = piece / 2; k > 0; k = k / 2)
		for (i = k; i < piece; i++)
		
			for (j = i - k; j >= 0 && array[j] > array[j+k]; j = j - k)
			{
				temp = new unsigned short;
				*temp= array[j];
				array[j] = array[j+k];
				array[j+k] = *temp;
				delete temp;
				for (int m = 0; m < piece; m++)
				{
					cout << " " << array[m] << " ";
				}
				cout << endl << endl;
			}
}
//---------------------------------------------
char Menu()
{
	auto char c;
	cout << "___________MENU___________" << endl;
	cout << "1. Swap " << endl << endl;
	cout << "2. Insertion "<< endl << endl;
	cout << "3. Bubble sorting " << endl << endl;
	cout << "4. Shell " << endl << endl;
	cout << "5. Exit " << endl << endl;
	cout << "__________" << endl;
	cout << "choose: "; cin >> c;
	
	cout << endl;
	return c;
}
//---------------------------------------------
int main()
{
	InitMemArray(9);
		// memory use
	do
	{
		array[0] = 7;array[1] = 4;
		array[2] = 9;array[3] = 3;
		array[4] = 2;array[5] = 8;
		array[6] = 5;array[7] = 1;
		array[8] = 6;
		switch ( Menu() )
		{
			case '1': Swap(9);
			break;
		
			case '2': Insertion(9);
			break;
			case '3': Bubble_Sorting(9);
			break;
			case '4': Shell(9);
			break;
			case '5': return 0;
			break;
			default:
			break;
		}
	} while (true);//( Menu() != '5');
	DelMemArray(9);
		// memory freeing
	return 0;
}


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/2/2007 4:36:59 PMBalogh

in zip:

http://bhsite.extra.hu/data/rendezes_source.zip
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.