Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class sortTest : MonoBehaviour
- {
- public Text[] scoreUI;
- public Color[] colors = { Color.black, Color.blue, Color.red, Color.green };
- List<int> score = new List<int> { 10, 20, 50, 30 };
- // 10, 20, 30, 50
- List<int> playerOrder = new List<int> { 0, 1, 2, 3 };
- // 0, 1, 3, 2
- private void Start()
- {
- bubbleSort(score);
- foreach (int pscore in score)
- {
- print("score: " + pscore);
- }
- foreach (int player in playerOrder)
- {
- print("player: " + player);
- }
- for (int i = 0; i < scoreUI.Length; i++)
- {
- scoreUI[i].text = "Player" + playerOrder[i] + " - " + score[i];
- scoreUI[i].color = colors[i];
- }
- }
- void bubbleSort(List<int> arr)
- {
- int n = arr.Count;
- for (int i = 0; i < n - 1; i++)
- for (int j = 0; j < n - i - 1; j++)
- if (arr[j] > arr[j + 1])
- {
- // swap scores
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- // swap the player order
- int ptemp = playerOrder[j];
- playerOrder[j] = playerOrder[j + 1];
- playerOrder[j + 1] = ptemp;
- // swap the color order
- Color ctemp = colors[j];
- colors[j] = colors[j + 1];
- colors[j + 1] = ctemp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement