The gunman who killed 12 people at the Washington Navy Yard on Monday began his rampage by heading directly to the fourth floor, where he shot people who worked with him, and authorities are investigating whether a workplace issue sparked the killings, according to law enforcement officials and witness accounts.

People in the department where Aaron Alexis was working had concerns about his job performance, and investigators are looking into whether those concerns escalated last week, the officials said.

“He was not doing a very good job, and somebody told him that there was a problem,” one law enforcement official said. “Our belief is that the people who were shot first were people he had issues with where he worked, people he had some sort of a dispute with. After that, it became random. . . . After the first shootings in that office, he moved around and shot people he came upon. They were then targets of opportunity.”

Alexis, a former Navy reservist who had recent problems with mental illness, was employed by a company contracted to upgrade computers at the Navy Yard.

Workers and law enforcement officials said Alexis worked on the fourth floor, where the shootings began. Although the investigators say they do not know the exact order in which the victims were shot, they said the rampage started in an area of people who would have worked with him.

Read eyewitness accounts from the Navy Yard shooting.

The officials cautioned that they are still trying to learn more about the severity of the dispute and whether it was an impetus for the shootings.

The law enforcement officials spoke on the condition of anonymity because the investigation is in its early stages. Three of the officials said most of the victims on the fourth floor were shot at close range and in the head.

Investigators noted that the shootings began on the floor where Alexis worked and not in the open lobby or the top floor where he could have fired down into the atrium below.

“We’re attempting to understand as best we can his life up until the moment of that shooting, which would include trying to understand whether there were any issues related to work,” FBI Director James B. Comey said Thursday at a briefing with reporters.

Officials with The Experts, the subcontractor that employed Alexis, declined to comment through a spokesperson.

Comey said Alexis arrived for work at about 8 a.m. Monday and parked on a deck across a narrow road from Building 197. Carrying a bag containing a Remington 870 shotgun, he entered the building and went to a bathroom on the fourth floor.

Both the stock and the barrel of the shotgun had been sawed to shorter lengths, making the weapon more compact and easier to wield, Comey said

“He shot folks on the fourth floor and the third floor,” Comey said. Then he walked downstairs to the lobby “and shot a security guard and took a weapon from the security guard — a Beretta semiautomatic pistol — and continued moving up and down through the building, focusing on the third and fourth floors.”

Comey said he viewed a surveillance video of parts of the shooting. “It appears to me that he was wandering the halls and hunting people to shoot,” said Comey, who was sworn in as FBI director this month and was speaking publicly about the mass shooting for the first time. “When you look at the folks who were shot, and those poor folks who lost their lives, they’re people from all different backgrounds from all over the building.”

For about a month, Alexis had been working in Building 197 with a team of other short-term subcontractors whose basic mission is relocating and installing computers, according to workers in that building who also spoke on the condition of anonymity.

Comey said it appears from the video that Alexis ran out of shotgun shells. So he began firing the pistol that he had taken from the slain security officer. “And that continued until the first responders arrived,” the FBI director said, referring to D.C. police and other law enforcement officers.

The officers “cornered him and had a sustained exchange of fire with him. And then he was downed and obviously killed at the scene.”

Comey said the investigation is ongoing. “We’re trying to make sure we understand in a careful and comprehensive way what happened and then what happened leading up to this,” he said.

Earlier employers of Alexis also noticed some issues. “The only thing that I thought was maybe out of the average is he kind of held on to grudges a little more than most people,” said Barry Williams, who was Alexis’s manager for the two years that he worked as an assistant in the administrative computing office at the Borough of Manhattan Community College. “Things that would bother him, they might be the same things that might bother other people, but three or four weeks later, even if it was a minor thing he’d still be grumbling about it.”

He could not remember any specific examples but said they were all work-related.

Paul Duggan, Peter Hermann, Carol D. Leonnig, Susan Svrluga and Julie Tate contributed to this report.