Untitled
unknown
plain_text
4 years ago
2.2 kB
13
Indexable
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string[] name = { "Todd", "Bob", "John", "Caitlin", "Katie", "Jennifer" };
string[] id = { "1100", "1010", "1001", "0011", "0101", "0110" };
Student[] library = new Student[6];
// create an array of students
for (int i = 0; i < library.Length; i++)
{
library[i] = new Student(name[i], id[i]);
}
Console.WriteLine("BEFORE SORT");
foreach (Student student in library)
Console.WriteLine(student); //use ToString() in Book class
// call selection sort on an array of student
InsertionSort(library);
// display array of student after sorting
Console.WriteLine("AFTER SORT");
foreach (Student student in library)
Console.WriteLine(student);
}
static public void InsertionSort(Student[] a)
{
for (int i = 1; i < a.Length; i++)
{
Student value = a[i]; //pick next value to insert
int j = i;
for (; j > 0 && value.CompareTo(a[j - 1]) == -1; j--) //cant have this this is for int
{
a[j] = a[j - 1]; // shuffle value rightwards in array
}
a[j] = value; // insert new value into sortedf side of array
}
}
}
class Student
{
private string name;
private int id;
public Student(string name, int id)
{
this.name = name;
this.id = id;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Id
{
get { return id; }
set { id = value; }
}
public int CompareTo(Object obj)
{
Student other = (Student)obj;
return Name.CompareTo(other.Name);
}
}
Editor is loading...