• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PvTGreg · Jan 12, 2015 at 12:11 PM · sort

bubble sort only display last found score

Hi when trying to write a bubble sort to run through my score it seems to only pick the last written part which is red tank why does it do this and how can i prevent it?

 using UnityEngine;
 using System.Collections;
 
 public class LeadingTanks : MonoBehaviour {
 
 
     int bluetank;
     int greentank;
     int redtank;
 
     public GameObject Text;
     // Use this for initialization
     void Start () 
     {
 
     }
 
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyDown(KeyCode.H))
         {
             bluetank = PlayerPrefs.GetInt("Blues Score");
             greentank = PlayerPrefs.GetInt("Greens Score");
             redtank = PlayerPrefs.GetInt("Reds Score");
             BubbleSort();
         }
     }
     void BubbleSort()
     {
         int[] arr = { bluetank,greentank,redtank };
         
         int temp = 0;
         
         for (int i = 0; i < arr.Length; i++) {
             for (int sort = 0; sort < arr.Length - 1; sort++) {
                 if (arr[sort] < arr[sort + 1]) {
                     temp = arr[sort + 1];
                     arr[sort + 1] = arr[sort];
                     arr[sort] = temp;
                 }
             }
         }
         foreach (int num in arr)
         {
             if(temp == bluetank)
             {
                 string myString = temp.ToString();
                 Text.GetComponent<TextMesh>().text = "Bluetank : " + myString;
             }
             else if(temp == greentank)
             {
                 string myString = temp.ToString();
                 Text.GetComponent<TextMesh>().text = "Greentank : " + myString;
             }
             else if(temp == redtank)
             {
                 string myString = temp.ToString();
                 Text.GetComponent<TextMesh>().text = "Redtank : " + myString;
             }
         }
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cocobongo · Jan 12, 2015 at 01:17 PM 0
Share

The Bubblesort looks fine. Your error seems to be in the foreach block. What are you trying to achieve here?

avatar image PvTGreg · Jan 12, 2015 at 03:19 PM 0
Share

im trying to display the tanks name (red,blue,green) before its score

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Cocobongo · Jan 14, 2015 at 07:48 AM

The temp variable holds the value of (one of) the last numbers swapped during the sort. It won't always be the highest value (consider how the sort runs when the initial array is e.g. [3,2,1])

Your algorithm is correct otherwise. Since you want the highest value, and your array is sorted in decreasing order, you will find the maximum at arr[0].

As a sidenote, if you only need the maximum, do not use a sorting algorithm. Performance is greatly impacted by these and you might just be better off with a max():

int max = Mathf.Max(bluetank,greentank,redtank);

Then get rid of the foreach() block and keep your if blocks.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PvTGreg · Jan 14, 2015 at 11:44 AM 0
Share

i need to use the bubble sort as its for a school project . i guess i just mis understood how the bubble sort works thanks

avatar image PvTGreg · Jan 14, 2015 at 11:52 AM 0
Share
 if(arr[0] == bluetank)
             {
                 string myString = arr[0].ToString();
                 Text.GetComponent<Text$$anonymous$$esh>().text = "Bluetank : " + myString;
             }
             else if(arr[0] == greentank)
             {
                 string myString = arr[0].ToString();
                 Text.GetComponent<Text$$anonymous$$esh>().text = "Greentank : " + myString;
             }
             else if(arr[0] == redtank)
             {
                 string myString = arr[0].ToString();
                 Text.GetComponent<Text$$anonymous$$esh>().text = "Redtank : " + myString;
             }



this works thanks

avatar image
0

Answer by code_warrior · Jan 12, 2015 at 02:40 PM

Hi,

Without knowing what you want to achieve its quite hard to tell.

But I think that the problem is that you compare the variable temp with your different tanktypes. Try num instead and also pass num to the variable myString.

The Bubblesort looks fine.

code_warrior

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PvTGreg · Jan 12, 2015 at 03:20 PM 0
Share

temp is the highest number im trying to get it to display the tank with the highest score and its name

avatar image Cocobongo · Jan 12, 2015 at 03:36 PM 0
Share

Well, since you already sort the array of scores, the maximum score will be found at the last position of your array (i.e. arr[arr.Length-1]).

The value of temp won't be the maximum; it contains only the value of the last score that was swapped which can be any value.

avatar image PvTGreg · Jan 13, 2015 at 06:16 PM 0
Share

well no since its a bubble sort once it finishes sorting temp will be the highest

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

switch between multiple cameras based on distance to target 2 Answers

Sort By Distance, Call it multiple times. 2 Answers

Show Top 10 Of 1 GameType 2 Answers

sorting list of key, value by value 1 Answer

Recreating the ModelViewProjection in Script ( as used in Shader ? ) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges