Bubblesort

bubblesort.sh

This is simply a demostration of how bubble sort works

​#!/bin/bash

inputresults()
{
  markFile="marks.txt"
  read -p "Input first student name: " name
  while [ "$name" != "xxx" ] ; do
    read -p "Input student mark: " mark
    echo "$name,$mark" >> $markFile
    read -p "Input next student name, (xxx to end) :" name
  done
}

readresults()
{
  read line < mark.txt
}

clear
studentNames=(" " " " " " " " " " " " " " " " " ")
studentMarks=(0 0 0 0 0 0 0 0 0)
set | grep student

markFile=marks.txt
echo "names and marks will be stored in two arrays"

for n in 9 8 7 6 5 4 3 2 1; do
   name=$(tail -$n $markFile | head -1| cut -d, -f1)
   mark=$(tail -$n $markFile | head -1| cut -d, -f2)
   (( pointer = n -1 ))
   studentNames[$pointer]=$name
   studentMarks[$pointer]=$mark
   # next n
done
echo "Arrays are now"

DEBUG=1
set | grep student

  for i in  0 1 2 3 4 5 6 7 8 ; do
    (( x = 8 - i ))
    j=0
    while (( j < x )) ; do
      (( j1 = j + 1 ))
      clear
      (( DEBUG == 1 )) && set | grep studentNames | grep ${studentNames[$j]}
      echo "if position $j ${studentNames[$j]} > position $j1 ${studentNames[${j1}]} then swap"
      if [[ "${studentNames[$j]}" > "${studentNames[${j1}]}" ]] ; then
            (( DEBUG == 1 )) && read && echo " Swapping ${studentNames[$j]} and ${studentNames[${j1}]}"
        temp=${studentNames[${j1}]}
        studentNames[${j1}]=${studentNames[$j]}
        studentNames[${j}]=$temp
        (( DEBUG == 1 )) && set | grep studentNames | grep ${studentNames[$j1]}
      else
        (( DEBUG == 1 )) && read && echo "Dont need to swap"
        (( DEBUG == 1 )) && set | grep studentNames | grep ${studentNames[$j]}
      fi
      (( DEBUG == 1 )) && read
      #echo  "$i, $j"
      (( j ++ ))
    done
  done

With marks.txt

Jo,65
Yan,88
Paul,76
Daisy,88
Mabel,80
Mavis,79
Anna,58
Billy,88
Keith,56