0% found this document useful (0 votes)
112 views5 pages

VB.NET Dinosaur List Operations

The document contains code examples in VB.NET, C#, and C++ that add dinosaur names to a list, sort the list, and use binary search to insert additional dinosaur names in the correct sorted position. The code outputs the list at each step to demonstrate the sorting and insertion.

Uploaded by

aravindwin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views5 pages

VB.NET Dinosaur List Operations

The document contains code examples in VB.NET, C#, and C++ that add dinosaur names to a list, sort the list, and use binary search to insert additional dinosaur names in the correct sorted position. The code outputs the list at each step to demonstrate the sorting and insertion.

Uploaded by

aravindwin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

VB.

NET
Imports System
Imports [Link]

Public Class Example

Public Shared Sub Main()

Dim dinosaurs As New List(Of String)

[Link]("Pachycephalosaurus")
[Link]("Amargasaurus")
[Link]("Mamenchisaurus")
[Link]("Deinonychus")

[Link]()
For Each dinosaur As String In dinosaurs
[Link](dinosaur)
Next

[Link](vbLf & "Sort")


[Link]

[Link]()
For Each dinosaur As String In dinosaurs
[Link](dinosaur)
Next

[Link](vbLf & _
"BinarySearch and Insert ""Coelophysis"":")
Dim index As Integer = [Link]("Coelophysis")
If index < 0 Then
index = index Xor -1
[Link](index, "Coelophysis")
End If

[Link]()
For Each dinosaur As String In dinosaurs
[Link](dinosaur)
Next

[Link](vbLf & _
"BinarySearch and Insert ""Tyrannosaurus"":")
index = [Link]("Tyrannosaurus")
If index < 0 Then
index = index Xor -1
[Link](index, "Tyrannosaurus")
End If

[Link]()
For Each dinosaur As String In dinosaurs
[Link](dinosaur)
Next
End Sub
End Class

' This code example produces the following output:


'
'Pachycephalosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'
'Sort
'
'Amargasaurus
'Deinonychus
'Mamenchisaurus
'Pachycephalosaurus
'
'BinarySearch and Insert "Coelophysis":
'
'Amargasaurus
'Coelophysis
'Deinonychus
'Mamenchisaurus
'Pachycephalosaurus
'
'BinarySearch and Insert "Tyrannosaurus":
'
'Amargasaurus
'Coelophysis
'Deinonychus
'Mamenchisaurus
'Pachycephalosaurus
'Tyrannosaurus

C#
using System;
using [Link];

public class Example


{
public static void Main()
{
List<string> dinosaurs = new List<string>();

[Link]("Pachycephalosaurus");
[Link]("Amargasaurus");
[Link]("Mamenchisaurus");
[Link]("Deinonychus");

[Link]();
foreach(string dinosaur in dinosaurs)
{
[Link](dinosaur);
}

[Link]("\nSort");
[Link]();

[Link]();
foreach(string dinosaur in dinosaurs)
{
[Link](dinosaur);
}

[Link]("\nBinarySearch and Insert \"Coelophysis\":");


int index = [Link]("Coelophysis");
if (index < 0)
{
[Link](~index, "Coelophysis");
}

[Link]();
foreach(string dinosaur in dinosaurs)
{
[Link](dinosaur);
}

[Link]("\nBinarySearch and Insert \"Tyrannosaurus\":");


index = [Link]("Tyrannosaurus");
if (index < 0)
{
[Link](~index, "Tyrannosaurus");
}

[Link]();
foreach(string dinosaur in dinosaurs)
{
[Link](dinosaur);
}
}
}

/* This code example produces the following output:

Pachycephalosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

Sort

Amargasaurus
Deinonychus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Coelophysis":


Amargasaurus
Coelophysis
Deinonychus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Tyrannosaurus":

Amargasaurus
Coelophysis
Deinonychus
Mamenchisaurus
Pachycephalosaurus
Tyrannosaurus
*/

C++
using namespace System;
using namespace System::Collections::Generic;

void main()
{
List<String^>^ dinosaurs = gcnew List<String^>();

dinosaurs->Add("Pachycephalosaurus");
dinosaurs->Add("Amargasaurus");
dinosaurs->Add("Mamenchisaurus");
dinosaurs->Add("Deinonychus");

Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}

Console::WriteLine("\nSort");
dinosaurs->Sort();

Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}

Console::WriteLine("\nBinarySearch and Insert \"Coelophysis\":");


int index = dinosaurs->BinarySearch("Coelophysis");
if (index < 0)
{
dinosaurs->Insert(~index, "Coelophysis");
}

Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}

Console::WriteLine("\nBinarySearch and Insert \"Tyrannosaurus\":");


index = dinosaurs->BinarySearch("Tyrannosaurus");
if (index < 0)
{
dinosaurs->Insert(~index, "Tyrannosaurus");
}

Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
}

/* This code example produces the following output:

Pachycephalosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

Sort

Amargasaurus
Deinonychus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Coelophysis":

Amargasaurus
Coelophysis
Deinonychus
Mamenchisaurus
Pachycephalosaurus

BinarySearch and Insert "Tyrannosaurus":

Amargasaurus
Coelophysis
Deinonychus
Mamenchisaurus
Pachycephalosaurus
Tyrannosaurus
*/

You might also like