C # implement a Bubble Sorting stack queue

Source: Internet
Author: User

Namespace bubblesort

{

Class bubblesorttest

{

/// <Summary>

/// Use nested loops for ascending sorting

/// </Summary>

/// <Param name = "arrary"> </param>

Public static void sort (INT [] arrary)

{

Int temp = 0;

For (INT I = 0; I <arrary. Length-1; I ++)

{

For (Int J = 0; j <arrary. Length-1-I; j ++)

{

If (arrary [J]> arrary [J + 1]) //Set">"To"<In descending order.

{

Temp = arrary [J];

Arrary [J] = arrary [J + 1];

Arrary [J + 1] = temp;

}

}

}

}

}

Class Program

{

Static void main (string [] ARGs)

{

Int [] arrary = {6, 4, 7, 2, 9, 1, 5, 12, 35, 74, 14, 57, 25, 75, 26, 95, 74, 35, 38, 73 };

Bubblesorttest. Sort (arrary );

Foreach (INT index in arrary)

{

Console. Write (index + "\ t ");

}

Console. readkey ();

}

}

}

 

 

 

QueueProgram

Namespace queue

{

Class queuetest

{

Int [] arrary;

Int length = 0;

Public queuetest (INT num)

{

Arrary = new int [num];

}

Public int Length

{

Get

{

Return this. length;

}

}

Public void push (INT num )//How to import data to the queue

{

If (length> 0)

{

For (INT I = length-1; I> = 0; I --)//Use a loop to move the existing data back to a location, and place the first bit to the data that just enters the queue. Read the data from the back to implement data first-in-first-out and later-out.

{

Arrary [I + 1] = arrary [I];

}

}

Arrary [0] = num;

Length ++;

}

Public int POP ()//Data output queue Method

{

Return arrary [-- length]; //Retrieve the last number.

}

}

Class Program

{

Static void main (string [] ARGs)

{

Queuetest = new queuetest (100 );

Queuetest. Push (4 );

Queuetest. Push (8 );

Queuetest. Push (6 );

Queuetest. Push (17 );

Queuetest. Push (2 );

Queuetest. Push (1 );

Queuetest. Push (9 );

While (queuetest. length> 0)

{

Console. Write (queuetest. Pop () + "\ t ");

}

Console. readkey ();

}

}

}

 

 

 

Stack Program

Namespace Stack

{

Class stacktest

{

Int [] arrary;

Int length = 0;

Public stacktest (INT num)

{

Arrary = new int [num];

}

Public int Length

{

Get

{

Return this. length;

}

}

Public void push (INT num )//Add new data to the back of old data,Pop ()Read from the back to achieve first-in-first-out, first-in-first-out

{

Arrary [length] = num;

Length ++;

}

Public int POP ()//Method of Data Output Stack

{

Return arrary [-- length]; //Retrieve the last number.

}

}

Class Program

{

Static void main (string [] ARGs)

{

Stacktest = new stacktest (100 );

Stacktest. Push (4 );

Stacktest. Push (8 );

Stacktest. Push (6 );

Stacktest. Push (17 );

Stacktest. Push (2 );

Stacktest. Push (1 );

Stacktest. Push (9 );

While (stacktest. length> 0)

{

Console. Write (stacktest. Pop () + "\ t ");

}

Console. readkey ();

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.