Question: JAVA sorting algorithim with JAVA SKELETON This assignment will implement a sorting algorithm; either bubble sort or exchange sort. Given an array of length N,

JAVA sorting algorithim with JAVA SKELETON

This assignment will implement a sorting algorithm; either bubble sort or exchange sort. Given an array of length N, the goal is to rearrange the elements such that they are in either nonincreasing or nondecreasing order. We say nondecreasing to signify that we can handle two elements may have the same value, such as the sorted array {1,2,5,5,7,7,8}. The elements in this array never decrease as we traverse from the beginning to end, so it is therefore sorted in nondecreasing array rather than simply ascending order, which would signify the elements are always increasing. The array will be sorted in-place, meaning that we will modify the existing array. This is in constrast to copying the array, and sorting the copy. Although this would allow us to have both sorted and original arrays, we generally only need this in certain circumstances. For example we might want to have one copy of the array sorted in nonincreasing order, and another in nondecreasing order. To complete this assignment, you must sort the phone numbers from the dataset on iLearn, and save these values to another file. Below is a helper template to do so. The main method calls loadFile method, which loads the file and returns an array of integer values. The main method then calls the sortArray method, which must be implemented in this assignment. It computes some timing information, to help gauge the efficiency of the algorithm. It then checks the results to ensure the array is, in fact, sorted. Finally, it saves the sorted data to a file by calling the saveDataToFile method. The data file is on iLearn in the HW5A section; it is every possible phone number starting with 8 in random order. There are a total of 1,000,000 numbers. Each number is stored as an int, and so the int array holding this data will require 4,000,000 bytes (roughly 4MB) of memory. Your task will be to sort the first 100,000 elements in this dataset. Warning: sorting the entire data file will take a long ( > 10 minutes) time. Sorting the first 100,000 elements should take less than a minute.

These are the contents of the Text file please create a text file and include the following contents and name the file : datafile.txt.rtf

1000000

8421029

8608815

8751948

8101634

8028721

8418128

8743039

8578404

8182463

8011969

8180293

8453170

8379252

8863405

8241634

8799601

8260774

8184197

8301626

8540238

8855331

8599520

8248346

8755484

8828485

8131368

8383434

8300393

8367700

8819687

8653885

8093055

8674031

8318546

8567192

8258847

8385734

8259314

8701315

8250930

8235514

8165067

8938770

8622049

8931136

8992099

8614456

8032951

8651865

8160769

8307045

8091979

8881609

8515134

8448738

8271167

8310750

8259011

8919944

8693285

8336012

8187117

8467634

8672851

8469132

8186408

8312434

8003389

8081318

8059661

8779312

8662576

8347193

8420061

8633741

8769624

8213404

8903304

8652195

8399241

8571806

8671298

8159444

8596355

8653865

8077239

8166381

8544808

8990893

8459775

8065684

8338421

8358848

8422516

8603046

8506948

8248659

8971354

8671371

8365379

8398412

8089066

8008524

8233376

8946290

8197617

8489058

8978957

8174510

8547929

8617813

8951897

8564136

8871899

8564439

8805883

8492568

8414543

8989541

8976686

8371196

8455556

8098803

8512175

8396273

8036022

8673563

8383693

8553018

8800953

8398284

8728001

8659591

8454530

8556876

8736000

8320041

8696495

8984906

8803662

8096170

8307312

8503232

8009166

8276228

8462840

8149822

8016949

8288243

8530216

8535169

8485959

8320231

8001638

8378171

8340919

8056783

8818452

8706332

8925334

8644664

8555872

8673381

8514329

8920919

8771965

8555367

8094900

8407831

8552290

8442108

8501415

8510489

8778108

8555467

8941842

8143175

8853904

8196610

8956153

8601695

8459918

8081781

8415294

8704185

8706053

8887545

8612873

8744119

8016341

8645310

8022304

8765220

8752377

8497346

8288871

8046479

8248002

8217495

8342138

8463413

8022090

8098521

8688569

8104271

8128860

8417033

8960138

8704808

8691138

8382683

8286928

8383657

8071776

8789628

8046786

8305836

8935874

8233484

8479994

8225936

8962712

8930848

8758205

8013626

8681199

8377053

8944519

8248719

8811908

8599504

8721272

8253245

8240478

8146254

8299720

8994888

8525169

8468482

8397218

8758386

8128702

8589476

8444582

8074453

8803548

8313006

8173752

8704474

8579309

8672592

8397176

8971840

8860871

8517691

8171852

8975433

8288824

8500947

8430074

8905560

8931884

8012564

8873017

8373020

8510669

8196040

8312066

8350601

8982622

8267435

8752807

8205311

8350697

8873983

8186037

8134679

8231401

8675006

8723886

8006804

8908658

8815296

8410818

8200494

8093214

8819470

8479630

8639353

8522378

8891315

8146624

8060134

8831866

8285767

8639287

8817113

8787290

8161437

8356851

8129215

8879076

8516921

8799135

8162584

8765659

8453736

8589822

8217923

8477193

8994217

8090554

8470977

8407615

8933031

8473872

8005340

8626537

8302714

8334022

8743075

8150463

8246302

8489176

8173785

8128735

8472399

8042986

8500038

8048613

8463676

8134628

8855455

8299546

8647791

8531116

8345035

8632900

8353942

8714686

8249727

8838097

8755566

8462977

8742880

8470153

8937572

8232717

8094915

8143166

8692183

8074953

8375577

8492421

8718404

8983480

8128628

8498228

8153328

8848311

8406265

8774018

8226304

8777822

8634031

8648623

8131841

8414038

8894801

8259811

8713401

8624459

8914823

8379705

8633508

8515166

8048127

8909395

8523685

8608774

8337863

8443683

8800999

8515440

8934552

8366672

8631976

8976472

8097318

8501706

8801449

8621637

8847344

8511375

8744646

8294208

8607846

8057590

8661980

8131415

8538548

8661700

8664897

8163668

8256082

8762083

8105169

8088778

8498528

8865174

8627749

8073717

8908720

8469696

8436566

8893120

8709119

8812822

8180273

8084544

8826132

8569418

8392036

8365100

8346913

8567057

8741607

8565604

8394963

8444071

8073691

8738539

8575469

8871827

8790542

8551695

8708861

8218968

8736270

8791386

8202117

8955511

8600138

8509921

8934817

8241968

8318521

8990011

8331638

8745993

8787708

8430484

8196258

8781724

8758510

8328252

8544644

8958131

8094905

8877553

8346899

8554305

8664978

8416408

8632265

8746106

8919716

8356160

8659119

8475468

8583730

8896607

8808256

8197743

8783413

8943216

8406646

8183399

8070086

8727366

8720408

8300014

8573997

8386242

8580435

8139006

8451660

8780782

8510251

8838211

8231653

8399458

8034905

8579493

8148244

8525463

8631332

8566860

8216402

8934090

8387089

8018608

8996391

8181750

8520418

8171638

8844855

8441194

8217470

8100075

8818114

8387108

8605809

8362614

8537166

8306205

8390079

8355272

8614506

8950602

8332035

8138970

8163418

8269029

8179366

8155600

8015605

8215864

8461140

8302831

8213479

8735992

8141019

8675588

8393918

8771707

8193191

8027031

8672589

8113654

8260533

8427802

8388768

8577957

8416927

8867488

8724854

8279398

8611988

8440670

8921388

8979648

8683568

8117387

8279233

8169932

8118599

8924268

8922860

8322489

8141314

8073431

8737845

8460079

8996733

8481289

8644124

8287935

8555303

8625308

8473059

8958212

8685480

8427834

8842996

8462984

8396932

8210625

8698999

8164530

8404369

8353749

8685533

8443328

8388822

8415611

8495367

8454833

8437331

8942644

8219959

8007287

8353811

8644166

8893809

8129817

8075761

8496102

8593590

8489877

8201296

8695582

8121714

8414812

8130161

8430334

8256566

8416780

8561158

8582129

8173410

8069605

8143650

8608373

8260361

8687610

8413551

8943021

8018079

8797958

8982965

8014012

8543637

8605947

8863747

8123137

8833580

8917485

8378566

8221738

8513734

8722811

8765273

8695560

8274914

8041753

8587626

8478347

8987623

8402490

8559448

8881724

8194688

8001287

8650749

8817144

8248096

8548424

8336855

8913439

8397358

8527176

8234482

8248293

8756124

8834822

8320280

8740913

8662442

8107535

8739557

8434326

8083020

8331880

8618753

8475862

8979864

8452195

8850265

8533777

8493828

8061143

8035194

8504249

8758798

8553347

8545217

8978901

8780012

8502753

8493765

8585390

8513581

8216334

8513019

8299568

8398350

8218779

8093747

8837448

8886623

8563311

8217147

8957029

8621549

8972824

8605524

8115575

8607550

8008350

8289468

8207074

8477572

8112529

8299819

8159014

8771303

8593492

8282007

8938688

8093154

8363566

8456938

8360539

8266238

8057626

8377501

8178172

8721846

8803401

8348853

8647088

8315608

8502132

8104736

8671566

8427337

8302166

8320873

8845179

8831038

8991089

8553629

8111057

8156792

8570742

8341975

8934400

8860582

8479158

8274608

8167584

8903342

8049576

8826973

8816563

8936034

8504634

8791128

8687002

8524574

8930440

8325220

8747924

8781658

8762579

8057991

8410019

8912158

8319430

8103939

8243637

8172459

8941738

8700725

8618048

8046997

8376816

8418894

8940241

8512202

8049250

8534900

8767554

8364345

8397410

8712452

8219638

8540521

8581946

8476212

8938199

8111757

8227247

8569724

8733290

8992941

8910614

8732300

8885621

8441542

8824571

8782905

8522608

8992523

8203915

8428370

8511091

8539862

8777704

8224231

8437437

8014020

8171341

8072154

8733130

8066327

8142257

8708911

8517215

8713988

8323646

8401695

8926629

8101557

8380098

8063741

8193086

8660331

8474603

8316678

8522331

8058970

8601305

8328319

8795957

8852377

8930341

8561569

8917983

8206487

8130752

8771410

8691574

8897357

8536557

8968690

8279524

8990318

8715245

8071311

8549498

8371897

8727574

8853232

8074544

8802270

8499074

8974367

8181243

8688140

8121062

8295256

8432774

8088203

8416046

8121490

8581528

8841939

8572564

8942929

8891105

8795648

8981422

8989212

8350860

8588826

8009715

8609889

8768021

8848202

8408214

8227097

8152598

8545170

8753808

8265759

8174239

8266006

8833487

8512115

8312431

8351764

8655057

8058992

8074026

8450525

8062512

8226563

8717104

8289390

8446114

8660425

8677213

8340183

8912733

8856417

8710644

8736055

8846762

8018241

8514409

8787033

8738709

8936248

8401089

8605630

8143446

8099884

8003743

8353679

8725727

8410979

8287977

8355962

8764913

8737178

8780284

8604887

8383652

8063806

8613025

8750655

8115022

8495591

8947283

8268959

8186150

8091238

8371025

8628456

8349168

8240228

8328189

8291021

8987777

8084071

8072656

8388648

8263500

8099434

8283313

8581604

8759367

8216740

8075152

8714803

8909196

8492297

8679322

8181875

8260035

8930130

8813034

8827131

8916935

8488497

8791640

8616316

8142614

8613077

8663219

8531843

8935104

8598505

8209704

8328481

8293333

8431765

8168962

8108485

8164325

8728415

8074982

8077755

8606115

8838136

8998474

8906211

8349108

8558715

8596882

8144667

8733403

8593527

8854468

8330610

8785392

8311642

8546764

8455023

8384881

8819907

8660240

8764196

8361418

8698148

8239352

8529480

8861521

8023786

8089604

8385667

8266276

8412419

8930699

8461596

8358428

8440756

8494355

8694602

8009657

8203688

8226344

8463997

8677726

8082687

8572742

8790031

8384103

8870063

8464195

/* SKELcsc210hw5B.java

*/

import java.util.Scanner;

import java.util.NoSuchElementException;

import java.io.PrintStream;

import java.io.IOException;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class SKELcsc210hw5B {

// sortArray: sort the array in-place, modifying the array.

// Only sort the first N items.

private static void sortArray(int[] arr, int N) {

// Implement this method

}

private static int[] loadFile(String fileName) {

int[] vals = null;

int sz = -1;

try {

Scanner sc = new Scanner(new FileInputStream(fileName));

if(sc.hasNextInt())

sz = sc.nextInt();

if (sz < 0) {

System.out.println("File not the right format!");

return vals;

}

vals = new int[sz];

for(int ii = 0; ii < sz; ii++) {

vals[ii] = sc.nextInt();

}

} catch (FileNotFoundException excptn) {

System.out.println( excptn.getMessage() );

} catch (NoSuchElementException excptn) {

System.out.println("Unexpected end of file!" );

vals = null;

}

return vals;

}

private static boolean saveDataToFile(int[] data, int N, String fileName) {

PrintStream fout;

try {

fout = new PrintStream(fileName);

} catch (IOException excptn) {

System.out.println( excptn.getMessage() );

return false;

}

fout.println(N);

for(int ii = 0; ii < N; ii++) {

fout.println(data[ii]);

}

return true;

}

public static void main (String[] args) {

String fName = "csc210hw5A-datafile.txt";

if(args.length > 0) {

fName = args[0];

}

int[] values;

System.out.println("Loading... " + fName);

if((values = loadFile(fName)) == null) {

System.out.println("Failed to read " + fName + ": Exiting.");

return;

}

System.out.println("Loaded " + fName + " successfully.");

String outFileName = "sorted.txt";

if(args.length > 1) {

outFileName = args[1];

}

System.out.println("Total elements loaded: " + values.length);

int N = 100000;

if(N > values.length)

N = values.length;

System.out.println("Sorting the first " + N + " elements.");

if(N > 100000) {

System.out.println("Warning: this could take several minutes!");

}

long startTime = System.currentTimeMillis();

sortArray(values, N);

long time = System.currentTimeMillis() - startTime;

long TimeMin = time / 60000;

time -= TimeMin*60000;

double TimeSec = (double)time / 1000.;

System.out.printf("Time taken: %d minutes, %.4f seconds ", TimeMin, TimeSec);

System.out.print("Checking data is in nondecreasing order... ");

int i;

for(i = 1; i < N; i++)

if(values[i-1] > values[i])

break;

if( i == N ) // if i is N, then we were able to get through entire loop

System.out.println("Yes *** HW5B Complete! ***");

else

System.out.println("No HW5B not complete. Sort error at index " + i);

System.out.println("Saving sorting results to " + outFileName);

if( saveDataToFile(values, N, outFileName) == false )

System.out.println("Could not save data correctly.");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!