Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Bubble Sort and Initialization not displaying

$
0
0
Hey guys ok so im now making a code i posted a few days ago it was dumped because they thought i was asking for the code when i simply needed to be pushed in the right direction but any way that doesnt matter i will now break down my code into segments trying to explain it the best to my ability ok so here we go


 //Display Array
        void ArrayData(int[] Numbers)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Console.Write("- {0} ",Numbers[i]);
            }
            Console.WriteLine();
            for (int z = 0; z < Numbers.Length; z++)
            {
                Console.Write("- {0} ",Numbers[z]);
            }

            for (int x = 0; x < Numbers.Length; x++)
            {
                Console.Write("- {0} ", Numbers[x]);
            }
            Console.WriteLine();
        }// end of array data

The array data now this is where most of it happens now when i get to the first line it was generating random numbers but it suppose to display the array from what im told and i have run it multiple times but once it gets to this part it just begins to generate phenomenal numbers which is the main issue with this code

Moving on

        static void Main(string[] args)
        {
            const int max = 10;
            int[] SearchArray = new int[max] ;
            int target = 0; //value to find
            int midpoint = 0;
            int first = 0; //first value in array
            int last = SearchArray.Length - 1; // last value in array
            bool ok = false;
            Program Display = new Program();
            Random GetRandom = new Random();


This is the main void with the values within the code


            //Initialize Array
            for (int x = 0; x < SearchArray.Length; x++)
            {
                SearchArray[x] = GetRandom.Next();
            Display.ArrayData(SearchArray);
            }
            //end initialize array


This is where the array initializes

            //sequential search
            Display.ArrayData(SearchArray);
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 1 - 9)");
                target = int.Parse(Console.ReadLine());
                if (target >= 1 && target <= 9)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            bool found = false;
            for (int z = 0; z < SearchArray.Length; z++)
            {
                if (SearchArray[target] == SearchArray[z])
                {
                    Console.WriteLine("Variable Found");
                    found = true;
                    break;
                }
            }

Now the Search this is where i search for the sequential search

            int i = 0, j = 0, temp = 0;
            //sorting: bubble sort
            Console.WriteLine("Bubble Sort initialized");
            Display.ArrayData(SearchArray);
            for (i = 0; i < SearchArray.Length; i++)
            {
                for (j = i + 1; j < SearchArray.Length; j++)
                {
                    if (SearchArray[i] > SearchArray[j])
                    {
                        temp = SearchArray[i];
                        SearchArray[i] = SearchArray[j];
                        SearchArray[j] = temp;
                    }
                    //Console.WriteLine("J={0}", j);
                }
                //Console.WriteLine("I={0}", i);
            }
            Display.ArrayData(SearchArray);
            Console.WriteLine("Bubble Sort completed");


Now this is where the bubble sort this is where it displays alot of its issues with displaying random numbers which i am not to sure why

            //*******************************************************************************
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 10 - 50)");
                target = int.Parse(Console.ReadLine());
                if (target >= 10 && target <= 50)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            //*******************************************************************************

now this is for the Binary search this seems to run fine for the time

            //change element begin
            int ChangeElement = 0;
            do
            {
                Console.WriteLine("What Element do you want to change?[0 - 10]");
                ChangeElement = ReadLine();
                if(ChangElement >0 && ChangeElement <= 10);
                    ok = true;
                else
                    Console.WriteLine("The value is not between 0 and 10");
                while(ok = false)
                Console.WriteLine("Enter the new value for element");
                SearchArray[ChangeElement] = GetInt();
            }

            //change element end

This is where the Element change begins and for some reason i am having issues with
else
statements if anyone could help with that would be greatly apprectiated

            while (first <= last)
            {
                midpoint = (first + last) / 2; //find midpoint of array
                if (target == SearchArray[midpoint])
                {
                    Console.WriteLine("The target has been found at {0} with a value of {1}", midpoint, SearchArray[midpoint]);
                    Display.ArrayData(SearchArray);
                    break;
                }
                else if (target < SearchArray[midpoint])
                    last = midpoint - 1;
                else
                    first = midpoint + 1;
            } //end of while loop
        }
    }
}

This is the Binary search which is having a few issues i hope to fix




This is the entire code as one
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
 * Name: NAME
 * Date: 13 Feb 2013
 * ID: ID
 */
namespace ConsoleApplication15
{
    class Program
    {
        //Display Array
        void ArrayData(int[] Numbers)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Console.Write("- {0} ",Numbers[i]);
            }
            Console.WriteLine();
            for (int z = 0; z < Numbers.Length; z++)
            {
                Console.Write("- {0} ",Numbers[z]);
            }

            for (int x = 0; x < Numbers.Length; x++)
            {
                Console.Write("- {0} ", Numbers[x]);
            }
            Console.WriteLine();
        }// end of array data

        static void Main(string[] args)
        {
            const int max = 10;
            int[] SearchArray = new int[max] ;
            int target = 0; //value to find
            int midpoint = 0;
            int first = 0; //first value in array
            int last = SearchArray.Length - 1; // last value in array
            bool ok = false;
            Program Display = new Program();
            Random GetRandom = new Random();

            //Initialize Array
            for (int x = 0; x < SearchArray.Length; x++)
            {
                SearchArray[x] = GetRandom.Next();
            Display.ArrayData(SearchArray);
            }
            //end initialize array
            //*******************************************************************************
            
            //sequential search
            Display.ArrayData(SearchArray);
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 1 - 9)");
                target = int.Parse(Console.ReadLine());
                if (target >= 1 && target <= 9)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            bool found = false;
            for (int z = 0; z < SearchArray.Length; z++)
            {
                if (SearchArray[target] == SearchArray[z])
                {
                    Console.WriteLine("Variable Found");
                    found = true;
                    break;
                }
            }
            //*******************************************************************************
            int i = 0, j = 0, temp = 0;
            //sorting: bubble sort
            Console.WriteLine("Bubble Sort initialized");
            Display.ArrayData(SearchArray);
            for (i = 0; i < SearchArray.Length; i++)
            {
                for (j = i + 1; j < SearchArray.Length; j++)
                {
                    if (SearchArray[i] > SearchArray[j])
                    {
                        temp = SearchArray[i];
                        SearchArray[i] = SearchArray[j];
                        SearchArray[j] = temp;
                    }
                    //Console.WriteLine("J={0}", j);
                }
                //Console.WriteLine("I={0}", i);
            }
            Display.ArrayData(SearchArray);
            Console.WriteLine("Bubble Sort completed");

            //*******************************************************************************
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 10 - 50)");
                target = int.Parse(Console.ReadLine());
                if (target >= 10 && target <= 50)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            //*******************************************************************************
            //change element begin
            int ChangeElement = 0;
            do
            {
                Console.WriteLine("What Element do you want to change?[0 - 10]");
                ChangeElement = ReadLine();
                if(ChangElement >0 && ChangeElement <= 10);
                    ok = true;
                else
                    Console.WriteLine("The value is not between 0 and 10");
                while(ok = false)
                Console.WriteLine("Enter the new value for element");
                SearchArray[ChangeElement] = GetInt();
            }

            //change element end
            //Binary Search
            // search sorted array with ascending values
            while (first <= last)
            {
                midpoint = (first + last) / 2; //find midpoint of array
                if (target == SearchArray[midpoint])
                {
                    Console.WriteLine("The target has been found at {0} with a value of {1}", midpoint, SearchArray[midpoint]);
                    Display.ArrayData(SearchArray);
                    break;
                }
                else if (target < SearchArray[midpoint])
                    last = midpoint - 1;
                else
                    first = midpoint + 1;
            } //end of while loop
        }
    }
}


If anyone could help with my issues that would be great

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>