0% found this document useful (0 votes)
2 views

FINAL-PROJECT-DSA-CS2A

The document outlines a final term project for the College of Communication and Information Technology, focusing on implementing a Quick Sort algorithm in C#. It details the program's structure, including input handling for sorting both numbers and characters, and provides code for sorting and displaying results. Additionally, it includes sections for valid and invalid input scenarios.

Uploaded by

deltavintage0926
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FINAL-PROJECT-DSA-CS2A

The document outlines a final term project for the College of Communication and Information Technology, focusing on implementing a Quick Sort algorithm in C#. It details the program's structure, including input handling for sorting both numbers and characters, and provides code for sorting and displaying results. Additionally, it includes sections for valid and invalid input scenarios.

Uploaded by

deltavintage0926
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

COLLEGE OF COMMUNICATION

AND INFORMATION
TECHNOLOGY

FINALTERM PROJECT
1ST TERM AY 2023-2024

Group Leader : Foronda, John Dave D.


Members : Aquino, Nyco Lance Dave C.
: Bañez, Billy Joene C.
: Jaurigue, Ram M.
: Tangonan, June Asshley C.
: Viduya, Zedrick John P.

Sorting Type : Quick Sort

Programming Language : C# (Console)


Program Code :

using SORT_01_CS2A;
using System;

class Program
{
static void Main()
{
string[] temp;
int[] intArray;
char[] charArray;

string num = "Numbers";


string ch = "Characters";

try
{
Console.Write("Enter the Amount of Items you want to Sort: ");
int size = Convert.ToInt32(Console.ReadLine());

temp = new string[size];

Console.WriteLine();

for (int i = 0; i < temp.Length; i++)


{
Console.Write("Enter ITEM [" + (i + 1) + "] :");
temp[i] = Console.ReadLine();
}

Console.WriteLine();

if (num == checkArray(temp))
{
intArray = new int[temp.Length];
for (int i = 0; i < temp.Length; i++)
{
intArray[i] = int.Parse(temp[i]);
}

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
AND INFORMATION
TECHNOLOGY

SortNumber sortNumber = new SortNumber(intArray);


sortNumber.displayQuickSort();
}
else if (ch == checkArray(temp))
{
charArray = new char[temp.Length];

string concatString = string.Join("", temp);

charArray = concatString.ToCharArray();

SortCharacter sortCharacter = new SortCharacter(charArray);


sortCharacter.displayQuickSort();
}
else
{
Console.WriteLine("Can't be Converted!");
}
}
catch (Exception ex)
{
Console.WriteLine (ex.Message.ToString());
}
finally
{
Console.ReadLine();
}
}

public static string checkArray(string[] array)


{
bool containsOnlyNumbers = true;
bool containsOnlyCharacters = true;

foreach (string str in array)


{
foreach (char c in str)
{
if (!char.IsLetter(c))
{
containsOnlyCharacters = false;
break;
}
}

if (!containsOnlyCharacters)
{
break;
}
}

foreach (string str in array)


{
foreach (char c in str)
{
if (!char.IsDigit(c))
{
containsOnlyNumbers = false;
break;

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
AND INFORMATION
TECHNOLOGY

}
}

if (!containsOnlyNumbers)
{
break;
}
}

if (containsOnlyNumbers)
{
return "Numbers";
}
else if (containsOnlyCharacters)
{
return "Characters";
}
else
{
return "Invalid";
}
}
}

using System;

namespace SORT_01_CS2A
{
internal class SortNumber
{
private int[] array;

public SortNumber(int[] array)


{
this.array = array;
}

public void displayQuickSort()


{
Console.WriteLine("Original Array:");
numPrintArray(array);

numQuickSort(array, 0, array.Length - 1);

Console.WriteLine("\nSorted Array:");
numPrintArray(array);
}

static void numQuickSort(int[] array, int low, int high)


{
if (low < high)
{
int partitionIndex = numPartition(array, low, high);

numQuickSort(array, low, partitionIndex - 1);


numQuickSort(array, partitionIndex + 1, high);

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
AND INFORMATION
TECHNOLOGY
}
}

static int numPartition(int[] array, int low, int high)


{
int pivot = array[high];
int i = low;

for (int j = low; j < high; j++)


{
if (array[j] <= pivot)
{
numSwap(array, i, j);
i++;
}
}

numSwap(array, i, high);

return i;
}

static void numSwap(int[] array, int i, int j)


{
if (i != j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;

numPrintArray(array);
}
}

static void numPrintArray(int[] array)


{
foreach (var element in array)
{
Console.Write(element + " ");
}
Console.WriteLine();
}
}
}

using System;

namespace SORT_01_CS2A
{
internal class SortCharacter
{
public char[] array;

public SortCharacter(char[] array)


{
this.array = array;
}

public void displayQuickSort()


{

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
AND INFORMATION
TECHNOLOGY

Console.WriteLine("Original Array:");
charPrintArray(array);

charQuickSort(array, 0, array.Length - 1);

Console.WriteLine("\nSorted Array:");
charPrintArray(array);
}

static void charQuickSort(char[] array, int low, int high)


{
if (low < high)
{
int partitionIndex = charPartition(array, low, high);

charQuickSort(array, low, partitionIndex - 1);


charQuickSort(array, partitionIndex + 1, high);
}
}

static int charPartition(char[] arr, int low, int high)


{
char pivot = arr[high];
int i = low;

for (int j = low; j < high; j++)


{
if (arr[j] < pivot)
{
charSwap(ref arr,ref arr[i], ref arr[j]);
i++;
}
}

charSwap(ref arr, ref arr[i], ref arr[high]);

return i;
}

static void charSwap(ref char[] arr, ref char a, ref char b)


{
if (a != b)
{
char temp = a;
a = b;
b = temp;

charPrintArray(array);
}
}

static void charPrintArray(char[] array)


{
foreach (char c in array)
{
Console.Write(c + " ");
}
Console.WriteLine();

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
} AND INFORMATION
TECHNOLOGY

}
}

Screenshots of Program Output (minimum of 5 test runs):

Valid Inputs:

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
AND INFORMATION
TECHNOLOGY

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
AND INFORMATION
TECHNOLOGY

Invalid Inputs:

College of Communication and Information


Technology: [email protected]
COLLEGE OF COMMUNICATION
AND INFORMATION
Putting Integers and Characters together will result to an error so
it can’t be converted. TECHNOLOGY

College of Communication and Information


Technology: [email protected]

You might also like